日期:2014-05-19  浏览次数:20815 次

DataGrid和Text绑定
新手,在Web页面中,如何做到点击DataGrid后,这条记录就相应的赋值给Text
比如,我的DataGrid中有“编号”“姓名”“性别”
也有三个相对应的Text,我想点中以后,把值赋给Text,怎么做啊?
还有怎么删除啊?就是选中我不想要的记录,然后删除?
谢谢了,本人是新手,问题比较简单,谢谢!
搜索很多这样的帖子了,都是看不明白,希望有源代码,谢谢!
再线等!

------解决方案--------------------
this.textBox1.Databindings.Add( "Text ",ds.tables[0], "ColName ");//绑定
------解决方案--------------------
在aspx页面
DataGrid里加DataKeyField属性,值一般为主键(如 DataKeyField= "id ")
加绑定列
<Columns>
<asp:ButtonColumn Text= "删除 " CommandName= "delete "> </asp:ButtonColumn>
</Columns>

注册DataGrid的DeleteCommand事件

在aspx.cs页面如下代码
private void DataGrid1_DeleteCommand(object source, DataGridCommandEventArgs e)
{
SqlConnection cn = new SqlConnection(你的数据库连接字符串);
string strDelete = "delete from 表名 Where id = @id ";
SqlCommand cmd = new SqlCommand(strDelete, cn);
cmd.Parameters.Add( "@id ", SqlDbType.VarChar).Value = DataGrid1.DataKeys[e.Item.ItemIndex].ToString();
cn.Open();
cmd.ExecuteNonQuery();
cn.Close();
DataGrid1.EditItemIndex = -1;
BindGrid();//这个方法要手写,重新绑定数据库连接
}
------解决方案--------------------
关键就是获得DataGrid1.DataKeys[e.Item.ItemIndex].ToString()这个主键

------解决方案--------------------
mark
------解决方案--------------------
mark