Gridview是实现数据绑定最常用的控件,可是公司用的是内部的自定义控件,用的久了就忘掉了使用已久的Gridview,毕竟求职还是要靠他的啊!
private void BindData()
{
DataTable dt = CommentaryViews.DataTables("summarytable='" + summarytable + "' and summaryid='" + summaryid + "' and delstatus=0", "");
dg.ShowHeader = false;
dg.DataSource = dt;
dg.DataBind();
}
protected void dg_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
dg.PageIndex = e.NewPageIndex;
BindData();
}
protected void dg_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string id = dg.DataKeys[e.Row.RowIndex].Value.ToString();
Commentary comm = new Commentary(id);
if (comm.Blleader == 1)
{
Label lab = e.Row.Cells[0].FindControl("lblContents") as Label;
lab.Text = "<font color=red>" + comm.Contents + "</font>";
}
TextBox txt = e.Row.Cells[0].FindControl("txtContents") as TextBox;
txt.Attributes["style"] += "display:none;";
//控制是否可编辑,删除
if (this.Opusr.Id != comm.Adder)
{
e.Row.Cells[1].Text = "";
}
if (!this.Opusr.MyPerm.Get(PERM.信息简报_领导评论权限_689))
{
e.Row.Cells[2].Text = "";
}
}
}
protected void dg_RowEditing(object sender, GridViewEditEventArgs e)
{
//先后顺序不能反
dg.EditIndex = e.NewEditIndex;
BindData();
Label lab = dg.Rows[dg.EditIndex].Cells[0].FindControl("lblContents") as Label;
lab.Attributes["style"] += "display:none;";
TextBox txt = dg.Rows[dg.EditIndex].Cells[0].FindControl("txtContents") as TextBox;
txt.Attributes["style"] = "";
}
protected void dg_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
Commentary comm = new Commentary(dg.DataKeys[e.RowIndex].Value.ToString());
comm.OperateUser = this.Opusr.Id;
comm.AppendColValue(comm.DelstatusColInfo, 1);
comm.Update();
BindData();
}
protected void dg_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
Commentary comm = new Commentary(dg.DataKeys[e.RowIndex].Value.ToString());
comm.OperateUser = this.Opusr.Id;
comm.AppendColValue(comm.ContentsColInfo, ((TextBox)(dg.Rows[e.RowIndex].Cells[0].FindControl("txtContents"))).Text.ToString().Trim());
comm.Update();
dg.EditIndex = -1;
BindData();
}
protected void dg_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
dg.EditIndex = -1;
BindData();
}
另外:对于一些command,比如delete,edit等是作为系统已用的操作,在命名的时候不能占用系统的commandname.
事件的触发顺序:显示itemcommand,然后才是具体的RowDeleting等,执行断点会发现,首先进入itemcommand,然后才是
RowDeleting,在itemcommand里面可以判断具体的commandname.
事项二:如果要对删除添加删除确定说明,可有如下方式:
1,在绑定的时候lb.Attributes.Add("onclick", "return confirm('您真的要删除此行吗?')");
2,转换成模板,然后编辑这个模板列,选中用于删除的Button,将其onClientClick属性设为
return confirm('您确认删除要删除么?').
事项三:另外,如果Gridview里外都需要进行验证,必须建立2个ValidationGroup.外面的很好解决,里面的也是对模版列,按钮列绑定ValidationGroup.如下:
<asp:TemplateField HeaderText="排序号">
<EditItemTemplate>
<asp:RangeValidator ID="RangeValidator2" runat="server" ControlToValidate="TextBox2"
Display="None" ErrorMessage="排序号必须为大于0的整数" MaximumValue="9999" MinimumValue="0"
SetFocusOnError="True" Type="Integer" ValidationGroup="update"></asp:RangeValidator>
<asp:TextBox ID="TextBox2" Width="40px" ValidationGroup="update" runat="server" Text='<%# Bind("indexid") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("indexid") %>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="10%" HorizontalAlign="Center" />
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" ValidationGroup="update">
<ItemStyle Width="13%" />
</asp:CommandField>
这是与上面对应的验证提示框:
<asp:ValidationSummary ID="ValidationSummary2" runat="server" ShowSummary="false"
ShowMessageBox="true" ValidationGroup="update" />
这是页面中另一个提示框:
<asp:ValidationSummary ID="ValidationSummary1" runat="server" ShowSummary="false"
ShowMessageBox="true" ValidationGroup="add" />
特殊说明:
利用 GridView自带的 “选择”,“删除”功能,在本机是中文,但是布置到服务器后却是英文,
可以在Page_Load事件中增加一下作为第一行代码,解决英文显示的问题:
(MyGridView.Columns[3] as CommandField).SelectText = "选择";
(MyGridView.Columns[4] as CommandField).DeleteText = "删除";
事项四:GRIDVIEW中对齐方式:
horizontalalign="center" verticalalign="Middle"