Gridview控件实现简单的绑定删除修改

Gridview是实现数据绑定最常用的控件,可是公司用的是内部的自定义控件,用的久了就忘掉了使用已久的Gridview,毕竟求职还是要靠他的啊!

ContractedBlock.gif ExpandedBlockStart.gif 绑定数据
        private void BindData()
        {
            DataTable dt 
= CommentaryViews.DataTables("summarytable='" + summarytable + "' and summaryid='" + summaryid + "' and delstatus=0""");

            dg.ShowHeader 
= false;
            dg.DataSource 
= dt;
            dg.DataBind();
        }
ContractedBlock.gif ExpandedBlockStart.gif 翻页实现
        protected void dg_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            dg.PageIndex 
= e.NewPageIndex;

            BindData();
        }
ContractedBlock.gif ExpandedBlockStart.gif 绑定时候的特殊处理
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 = "";
                }
            }
        }
ContractedBlock.gif ExpandedBlockStart.gif 开始编辑
  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"= "";
        }
ContractedBlock.gif ExpandedBlockStart.gif 删除操作
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();
        }
ContractedBlock.gif ExpandedBlockStart.gif 更新操作
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();
        }
ContractedBlock.gif ExpandedBlockStart.gif 取消编辑
        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.如下:

ContractedBlock.gif ExpandedBlockStart.gif Code
  <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"

 

转载于:https://www.cnblogs.com/yongxingxie/archive/2009/02/28/1400327.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值