做个循环,遍历DATAGRID的所有行
解决方案 »
- 创建cookies的问题,请教高手。
- ASP.net菜鸟询问,怎么在网页上添加控件
- 再开一贴:if ((new AccessTask()).CheckUserPwd(YongHuMing.Text.Trim()) == MiMa .Text.Trim ())
- 求 ASP.NET WEB控件的书有没有
- 终于碰到一个不菜的问题(<%#Container.DataItem%>),可是没有分了:(
- 运行工程项目中的aspx文件时总出现以下错误提示:
- activeX客户端注册问题
- 求符合小数的正则表达式写法!!
- 要疯了,saveAs,上传的问题。
- 求.net下的TreeView带有右键(Web)????!!!!!!!!!!!
- 使用DataGrid如何进行分页?
- 求正则表达式匹配中文,英文字母和数字及_的写法!!
然后嘛
在checkbox状态改变的事件里对它所处的行进行处理咯
private void btnUpdate_Click(object sender, System.EventArgs e)
{
// Loop through the items in the datagrid.
foreach (DataGridItem di in dgContacts.Items)
{
// Make sure this is an item and not the header or footer.
if (di.ItemType == ListItemType.Item || di.ItemType == ListItemType.AlternatingItem)
{
// Get the current row for update or delete operations later.
DataRow dr = _dsContacts.Tables["Contact"].Rows.Find(dgContacts.DataKeys[di.ItemIndex]); // See if this one needs to be deleted.
if (((CheckBox)di.FindControl("chkDelete")).Checked)
{
_dsContacts.Tables["Contact"].Rows.Remove(dr);
}
else
{
// Update the row instead.
dr["Email"] = ((TextBox)di.FindControl("Email")).Text;
dr["FirstName"] = ((TextBox)di.FindControl("First")).Text;
dr["LastName"] = ((TextBox)di.FindControl("Last")).Text;
}
}
} // Save the changes if there are any.
if (_dsContacts.HasChanges())
{
SaveContacts();
} BindContacts();
}
1)首先在datagrid的模板列中放入checkbox ,并且赋予id号(我的程序是CheckBox1)。
2)遍历所有的行,找到被选中的行。
3)取出作下一步操作。
下面是vb代码 For i = 0 To Me.myDataGrid.Items.Count - 1
Dim checkbox1 As CheckBox
'查找CheckBox控件
checkbox1 = Me.myDataGrid.Items(i).FindControl("CheckBox1")
If (checkbox1.Enabled = True And checkbox1.Checked) Then
取出相关的数值,作下一步操作。
End If
Next
今天早上我又调试了一下你说的问题。
似乎在翻页的情况下问题不太好解决。主要问题是在使用翻页的过程中,数据表格要重新绑定。而且到新的一页后。 Me.myDataGrid.Items.Count 要重新赋值。原来选中的状态不保存。
我给你一个思路,你参考一下。
使用一个公共数组对象。public selectitems(20,10) as string
20代表页数。比如: selectitems(2,3)代表第一页第3行的数据。
用来存放已选中的项目所在的页和所选项目的唯一标识ID(或是其他数据)。
在翻页事件触发时,在改变页PAGEINDEX之前,将本页所选项填入数组。在放入session.
用session来保存和传递数据。