给GridView添加行单击事件

功能:
  
  1:添加行单击事件
  2:获取行里面的单元格值
  
  
  主要代码:
   protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
   {
   Button btnHiddenPostButton = e.Row.FindControl("btnHiddenPostButton") as Button;
   if (btnHiddenPostButton != null) {
   e.Row.Attributes["onclick"] = String.Format("javascript:document.getElementById('{0}').click()", btnHiddenPostButton.ClientID);
   // 额外样式定义
   e.Row.Attributes["onmouseover"] = "javascript:this.style.background='red'";
   e.Row.Attributes["onmouseout"] = "javascript:this.style.background=''";
   e.Row.Attributes["style"] = "cursor:pointer";
   e.Row.Attributes["title"] = "单击选择当前行";
   }
   // 若希望将隐藏按钮单独放于一列,则设置此列隐藏,占位符 <cellIndex> 表示此列索引
   //e.Row.Cells[<cellIndex>].Attributes["style"] = "display:none";
   }
  
   protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
   {
  
   GridViewRow row = null;
  
   Label lbl = null;
   switch (e.CommandName) {
   case "HiddenPostButtonCommand": // 模板列
   Control cmdControl = e.CommandSource as Control; // 表示触发事件的 IButtonControl,保持统一性并便于后续操作,我们这里直接转化为控件基类 Control
   row = cmdControl.NamingContainer as GridViewRow; // 当前行
   // 如何访问单元格值
   // string txt = row.Cells[0].Text;
   // 如何获取模板列中的 Label
   lbl = row.FindControl("MyLabelID") as Label;
   // 执行更多的自定义操作
   //
   //
   string txt = lbl.Text.ToString();
   Response.Write(txt);
   Response.Write(String.Format("GridView Version 当前第 {0} 行:", row.RowIndex + 1));
   break;
   // case "Command2":
   // more cases
   //
   }
   } 
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在使用DevExpress GridControl控件中,如果需要添加,可以通过以下步骤实现: 1. 首先,在GridControl控件中选中想要添加的数据源,比如DataTable、List等。 2. 然后,在GridControl控件的Designer视图中设置控件的AllowAddNewRow属性为True。 3. 接着,在代码中调用GridView控件的AddNewRow方法,启动添加操作。 4. 在GridView控件的InitNewRow事件中,对新初始化,比如设置默认值等。 5. 最后,将新添加到数据源中,刷新GridView的数据显示。 下面是示例代码: 1. 在GridControl控件的Designer视图中设置AllowAddNewRow属性为True: ![image](https://user-images.githubusercontent.com/8575679/137455672-6dfeefa6-0ed6-4c6b-8d1e-0cfa34dde057.png) 2. 在代码中调用GridView的AddNewRow方法启动添加操作: ``` gridView1.AddNewRow(); ``` 3. 在GridView的InitNewRow事件中进的初始化: ``` private void gridView1_InitNewRow(object sender, DevExpress.XtraGrid.Views.Grid.InitNewRowEventArgs e) { gridView1.SetRowCellValue(e.RowHandle, "ID", 0); gridView1.SetRowCellValue(e.RowHandle, "Name", "New Row"); } ``` 这里通过SetRowCellValue方法设置新的ID和Name属性值。 4. 最后,在代码中将新添加到数据源中,刷新GridView的数据显示: ``` dataTable.Rows.Add(gridView1.GetDataRow(gridView1.FocusedRowHandle)); gridView1.RefreshData(); ``` 这里通过Add方法将新添加到DataTable中,然后调用GridView的RefreshData方法刷新数据显示。 以上就是在DevExpress GridControl控件中添加的步骤,实现起来相对简单。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值