GridView edit when bound to a Datatable

故事扮演 提交于 2019-12-24 10:12:16

问题


I am creating a website where our customers can order parts directly from us. I have a datatable setup and when users click a button it adds a quick detail of the order to a gridview. In the gridview, I have the edit and delete buttons enabled. the delete function works fine, however when you try to edit the information, it doesn't change the gridview with the new info. Here's what I have so far:

protected void griditems_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
  DataTable dt = (DataTable)Session["table"];
  foreach (DataRow dr in dt.Rows)
    {
        part = Convert.ToString(dr["Part"]);
        dr["Part"] = part;
        dr["Quantity"] = qty;
        dr["Ship-To"] = shipto;
    }

    griditems.EditIndex = -1;
    BindData();
}

when trying this, it displays the gridview back with the original input values. I have also tried this (not working and get an error that says "There is no row at position 0":

DataTable dt = (DataTable)Session["table"];
GridViewRow row = griditems.Rows[e.RowIndex];
dt.Rows[row.DataItemIndex]["Part"] = ((TextBox)(row.Cells[1].Controls[0])).Text;
dt.Rows[row.DataItemIndex]["Quantity"] = ((TextBox)(row.Cells[2].Controls[0])).Text;
dt.Rows[row.DataItemIndex]["Ship-To"] = ((CheckBox)(row.Cells[3].Controls[0])).Checked;

griditems.EditIndex = -1;
BindData();

Am I missing an EditItemTemplate in the aspx file, or am I just doing the RowUpdating all wrong?


回答1:


You probably need to step back a bit and first check out how you could do create, update, delete and read with a grid view. Also, you may wanna check this post.



来源:https://stackoverflow.com/questions/12146488/gridview-edit-when-bound-to-a-datatable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!