绑定到Datatable时的GridView编辑(无数据库)

时间:2014-01-13 16:53:21

标签: asp.net gridview

我是ASP.NET的初学者,我一直试图允许编辑/更新绑定到Datatable的GridView,但没有取得明显的成功。

名为“WeightGridView”的GridView有两列,“资产类”和“权重”,并在点击“AddAssetsButton”按钮时填充。单击此按钮后,GridView将填充列表框“ListBox2”的值。此事件处理程序的代码是:

 protected void AddAssetsButton_Click(object sender, EventArgs e)
 {
        DataTable table = new DataTable();
        table.Columns.Add("Asset Class", typeof(string));
        table.Columns.Add("Weight", typeof(double));
        foreach (ListItem li in ListBox2.Items)
        {
            DataRow dr = table.NewRow();
            dr["Asset Class"] = li.Value;
            dr["Weight"] = 0;
            table.Rows.Add(dr);
        }
        WeightGridView.DataSource = table;
        WeightGridView.DataBind();
        SelectAssetsButton_ModalPopupExtender.Hide();
 }

我为GridView创建了一个RowEditing和RowUpdating函数,但它们似乎无法正常工作。

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            WeightGridView.DataBind();
        }
    }

    protected void WeightGridView_RowEditing(object sender, GridViewEditEventArgs e)
    {
        WeightGridView.EditIndex = e.NewEditIndex;
    }

    protected void WeightGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string weight;
        weight = ((TextBox)WeightGridView.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
        WeightGridView.EditIndex = -1;
        WeightGridView.DataBind();
    }

如果您需要任何其他代码来回复此帖子,请告知我们,我会更新帖子。

任何帮助将不胜感激!谢谢!

0 个答案:

没有答案