日期:2014-05-18  浏览次数:20443 次

DataGrid中按钮触发事件问题 急求
DagaGrid1显示的内容如下:

学号     姓名     网站     投票(按钮)

0001     小李     xxx       【button】  


如何在这里点击     【button】以后   使得数据库Student表中对应这行记录(0001)中的     票数     字段加   1   呢?

最好详细点!谢谢了

------解决方案--------------------
button属性:
CommandArgument= ' <%# DataBinder.Eval(Container.DataItem, "id ")%> '
CommandName= "upId "

Grid_ItemCommand事件中:
if(e.CommandName== "upId ")
{
//e.CommandArgument当前行ID
}
------解决方案--------------------
//aspx文件
<asp:DataGrid id= "DataGrid1 " runat= "server " DataKeyField= "id " ...
这个id是你的表的主键
<asp:ButtonColumn Text= "选择 " CommandName= "select "> </asp:ButtonColumn>
CommandName随便取,只要不是edit,update,delete和cancel

//aspx.cs文件
//在ItemCommand事件中做处理
private void DataGrid1_ItemCommand(object source, DataGridCommandEventArgs e)
{
if(e.CommandName == "select ")
{
//可以使用DataGrid1.DataKeys[e.Item.ItemIndex].ToString();得到对应项的主键值,然后你可以做你的更新操作
}
BindDataGrid();//最后别忘了重新绑定
}

------解决方案--------------------
都让高歌说了
啰唆一句吧

使用DataGrid1.DataKeys[e.Item.ItemIndex].ToString();时,在绑定datagrid时要定义主键

this.DataGrid1.DataKeyField= "学号 ";