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

GRIDVIEW控件绑定数据源,怎样取得字段的值?
点击GRIDVIEW中的修改``怎样取得那一行中的某个绑定的字段值

------解决方案--------------------
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds;
this.GridView1.DataKeyNames = new string[] { "mm_ID" };//主键值
 
GridView1.DataBind();
-------------------------------------
//找出要修改的这个行的主键值
int id = Convert.ToInt32(GridView2.DataKeys[e.RowIndex].Values[0].ToString());

------解决方案--------------------
GridView编辑时获取编辑框中的值
string quantity = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text; 

 protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "check")
{
string index= e.CommandArgument.ToString();
GridViewRow row = GridView1.Rows[index];
string id = row.Cells[1].Text;
}
}
直接............
------解决方案--------------------
首先在GridVieW的DatakeyNames属性中设置你需要的字段名,
<asp:GridView ID="gvwInformationManage" runat="server" AllowSorting="True" AutoGenerateColumns="False" OnRowCommand="gvwInformationManage_RowCommand" DataKeyNames = "id,projectid,postid,state,amount,time">



下面在程序中获得:
protected void gvwInformationManage_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Edit")
{
int index = Convert.ToInt32(e.CommandArgument); //获得当前行的索引,
DataKey data = gvwInformationManage.DataKeys[index];//根据索引获取当前行的DataKey,这个DataKey就是根据DataKeyNames中的设置来的
string str = data.Values["projectid"].ToString();//假设在DataKeyNames中指定了projectid
}
}