您的位置:首页 > 其它

J2EE基础--究竟什么是POJO

2011-08-08 10:58 309 查看
GridView用法:
1、分页
属性:AllowPaging(设置是否允许分页) PageSize (设置每页的数量条数)。
GridView的PageIndexChanging事件
GridView1.PageIndex = e.NewPageIndex; //e是系统自定的
GrdBindData(); //重新绑定数据

2、删除数据(RowDeleting事件),修改数据(RowUpdating事件)
string CustomerID = GridView1.DataKeys[e.RowIndex].value.tostring();
DeleteCustomers(CustomerID);
/*如果是修改数据的话
如果不加上IsPostBack,取得的值是数据库中的字段,而不是GridView表格中TextBox的值,但如果加上IsPostBack就一切正常.
string CustomerID = GridView1.DataKeys[e.RowIndex][0].ToString();
string CompanyName = ((TextBox)GridView1.Rows[e.RowIndex].Cells[0].Controls[0]).Text;
string ContactName = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
UpdateCustomers(CustomerID, CompanyName, ContactName, Address);
GridView1.EditIndex = -1;
*/
GrdBindData(); //重新绑定数据

3、进入编辑状态(RowEditing事件)
this.GridView1.EditIndex = e.NewEditIndex;
GrdBindData(); //重新绑定数据

4、终止编辑状态(RowCancelingEdit事件)
this.GridView1.EditIndex = -1;
GrdBindData(); //重新绑定数据

5、鼠标移到GridView某一行时改变该行的背景色的方法,添加行号的方法(RowDataBound事件)
//首先判断是否是数据行
if (e.Row.RowType == DataControlRowType.DataRow)
{
//当鼠标停留时更改背景色
e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#00A9FF'");
//当鼠标移开时还原背景色
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
////当有编辑列时,避免出错,要加的RowState判断
//if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
//{
// ((LinkButton)e.Row.Cells[3].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('您确定要删除:\"" + e.Row.Cells[1].Text + "\"吗?')");
//}
}
//添加行号,这时最好把前台的第一列的表头该为“编号”,因为以前的第一列被“吃掉”了。
if (e.Row.RowIndex != -1)
{
int id = e.Row.RowIndex + 1;
e.Row.Cells[0].Text = id.ToString();
}

6、GridView实现用“...”代替超长字符串 方法:数据绑定后过滤每一行即可
for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
{
string gIntro;
if (GridView1.PageIndex == 0)
{
DataTable vDt = SqlHelper.GetDataTable();
gIntro = Convert.ToString(vDt.Rows[i][2]);//所要处理的字段
GridView1.Rows[i].Cells[3].Text = SubStr(gIntro, 2);
}
//截取字符的方法
public string SubStr(string sString, int nLeng)
{
if (sString.Length <= nLeng)
{
return sString;
}
string sNewStr = sString.Substring(0, nLeng);
sNewStr = sNewStr + "...";
return sNewStr;
}

7、每行添加Checkbox,添加模板列即可
<asp:TemplateField>
<HeaderTemplate>选择</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
</asp:TemplateField>

8、全选的checkbox的CheckedChanged事件
for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
{
CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
if (CheckBox2.Checked == true)
{
cbox.Checked = true;
}
else
{
cbox.Checked = false;
}
}
9、删除所选的Button的onclick事件
int vNum=0;
using (SqlConnection conn = new SqlConnection(SqlConnStr))
{
conn.Open();
for (int i = 0; i < GridView1.Rows.Count - 1; i++)
{
CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
if (cbox.Checked == true)
{
DelSelect(Convert.ToInt32(GridView1.DataKeys[i].Value));
vNum++;
}
}
}
GridDataBind();
ClientScript.RegisterStartupScript(this.GetType(), "", string.Format("<script language='javascript' type='text/javascript'>alert('删除了{0}条记录');</script>", vNum));

10、在winform中gridview改了个名字叫DataGridview,绘制行号的方法如下:
private void gvdata_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{

Rectangle rectangle = new Rectangle(e.RowBounds.Location.X,
Convert.ToInt32(e.RowBounds.Location.Y + (e.RowBounds.Height - gvdata.RowHeadersDefaultCellStyle.Font.Size) / 2),
gvdata.RowHeadersWidth - 4, e.RowBounds.Height);
TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(),
gvdata.RowHeadersDefaultCellStyle.Font, rectangle, gvdata.RowHeadersDefaultCellStyle.ForeColor,
TextFormatFlags.Right);
}

11、gridview空数据时 也显示列头
<asp:GridView ID="GridView1" runat="server" >
<EmptyDataTemplate>
<table>
<tr>

<th>Id</th>
<th>Name</th>
<th>Department</th>
</tr>
<tr>
<td colspan="3">
对不起,没有找到任何相关记录
</td>
</tr>
</table>
</EmptyDataTemplate>
<Columns>
<asp:TemplateField>
<HeaderTemplate>ID</HeaderTemplate>
<ItemTemplate><%#eval_r("Id") %></ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>Name</HeaderTemplate>
<ItemTemplate><%#eval_r("Name") %></ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate> Department</HeaderTemplate>
<ItemTemplate><%#eval_r("Department") %></ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: