2013-04-06 66 views
0

我有网格视图,其中我想更新行,但它没有发生。数据源是一个DataTable。请帮忙。GridView行不更新,从GridView中的文本框中的文本编辑模板不来


下面是标记

<asp:GridView ID="GrdV" runat="server" AutoGenerateColumns="false" 
    OnRowEditing="GrdV_RowEditing" OnRowUpdating="GrdV_RowUpdating"> 

<Columns> 
    <asp:TemplateField HeaderText="Clip Description"> 
     <ItemTemplate> 
      <asp:Label ID="lblDescrptn" runat="server" Text='<%# Bind("Description") %>'> 
      </asp:Label> 
     </ItemTemplate> 
     <EditItemTemplate> 
      <asp:TextBox ID="descTbx" runat="server" Text='<%# Bind("Description") %>'> 
      </asp:TextBox> 
     </EditItemTemplate> 
    </asp:TemplateField> 

    <asp:CommandField ShowEditButton="True" /> 

</Columns> 

,这背后是

protected void GrdV_RowUpdating(object sender, GridViewUpdateEventArgs e) 
    { 

     // Retrieve the row being edited. 
     int index = GrdV.EditIndex; 
     GridViewRow row = GrdV.Rows[index];   
     TextBox t1 = row.FindControl("descTbx") as TextBox; 

     DataTable dt = (DataTable)Session["tmdataTable"]; 

     dt.Rows[index]["Description"] = t1.Text; //Description is a column of my DataTable 
     dt.AcceptChanges(); 
     GrdV.EditIndex = -1; 
     GrdV.DataSource = dt; 
     GrdV.DataBind(); 

    } 

在调试代码,我发现文本框被传递空字符串t1.Text =""即使我已经填写文本框用新值。 我认为错误是本着

TextBox t1 = row.FindControl("descTbx") as TextBox; 

pageLoad的代码

protected void Page_Load(object sender, EventArgs e) 
    { 

     if (!IsPostBack) 
     { 
      GrdV.DataSource = Session["tmdataTable"]; 
      GrdV.DataBind(); 
     } 


     DataTable Finaldt = getTable(); 

     GrdV.DataSource = Finaldt; 
     GrdV.DataBind(); 
     Session["tmdataTable"] = Finaldt; 

    } 

回答

0

改变你的代码位和检查。更改EditIndexe.RowIndex

protected void GrdV_RowUpdating(object sender, GridViewUpdateEventArgs e) 
{ 
    GridViewRow row = GrdV.Rows[e.RowIndex]; // this line is changed  
    TextBox t1 = row.FindControl("descTbx") as TextBox; 

    DataTable dt = (DataTable)Session["tmdataTable"]; 

    dt.Rows[row.DataItemIndex]["Description"] = t1.Text; //Description is a column of my DataTable 
    dt.AcceptChanges(); 
    GrdV.EditIndex = -1; 
    GrdV.DataSource = dt; 
    GrdV.DataBind(); 
} 

你这样做:

protected void Page_Load(object sender, EventArgs e) 
{  
    if(!IsPostBack) 
    { 
     GrdV.DataSource = Session["tmdataTable"]; 
     GrdV.DataBind(); 
    } 
} 
+0

无效果需要e.RowIndex。仍然t1.text是空白 – 2013-04-06 07:34:21

+0

看到我的更新。 – 2013-04-06 07:43:19

+0

仍然没有什么,同样的问题 – 2013-04-06 07:51:55

0
protected void GrdV_RowUpdating(object sender, GridViewUpdateEventArgs e) 
    { 
     if (e.Item is GridDataItem) 
     { 
     // Retrieve the row being edited. 
     int index = GrdV.EditIndex; 
     GridViewRow row = GrdV.Rows[index];   
     TextBox t1 = row.FindControl("descTbx") as TextBox; 

     DataTable dt = (DataTable)Session["tmdataTable"]; 

     dt.Rows[index]["Description"] = t1.Text; //Description is a column of my DataTable 
     dt.AcceptChanges(); 
     GrdV.EditIndex = -1; 
     GrdV.DataSource = dt; 
     GrdV.DataBind(); 
     } 
    } 
1

EditIndex不可用,你从GridViewUpdateEventArgs

// Retrieve the row being edited. 
DataTable dt = (DataTable)Session["tmdataTable"]; 
GridViewRow row = GrdV.Rows[e.RowIndex]; 
TextBox t1 = row.FindControl("descTbx") as TextBox; 

dt.Rows[row.DataItemIndex]["Description"] = t1.Text; //Description is a column of my DataTable 
dt.AcceptChanges(); 
GrdV.EditIndex = -1; 
GrdV.DataSource = dt; 
GrdV.DataBind(); 
+0

没有影响。仍然在t1.Text中传递空白值。 – 2013-04-06 07:36:34

+0

尝试将'GrdV.DataSource = dt;'更改为'GrdV.DataSource = Session [“tmdataTable”];'但是再次,这看起来像Praveen所说的 – mcalex 2013-04-06 08:12:20