简体   繁体   English

动态绑定asp.net gridview列表单文本框值

[英]bind asp.net gridview column form textbox value dynamically

I want to bind gridview column from textbox value for example: I have two textbox and a button what i exactly want that i will give input in the two textboxes and when i clicked the button the textbox values will show in gridviw and want to do this for multiple times without replacing the previous values.我想从文本框值绑定 gridview 列,例如:我有两个文本框和一个按钮,我完全想要我将在两个文本框中输入,当我单击按钮时,文本框值将显示在 gridviw 中并想要这样做多次而不替换以前的值。

I found so many relevant answer but all are done with hard code inside button click event我找到了很多相关的答案,但都是在按钮点击事件中使用硬代码完成的

protected void Button1_Click(object sender, EventArgs e)
{
    DataTable dt = new DataTable();
    dt.Columns.AddRange(new DataColumn[2] { new DataColumn("Name",  
    typeof(string)),
    new DataColumn("Age", typeof(decimal)) });
    dt.Rows.Add(TextBox1.Text, TextBox2.Text);
    GridView1.DataSource = dt;
    GridView1.DataBind()
}

which replace the previous values when i insert new values, but i want to keep all the previous values in which I stuck .thanks当我插入新值时替换以前的值,但我想保留我卡住的所有以前的值。谢谢

this code will help you to get current data from gridview to datatable and then you can append new rows accordingly .此代码将帮助您将当前数据从 gridview 获取到数据表,然后您可以相应地附加新行。

protected void Button1_Click(object sender, EventArgs e)
    {
    DataTable dt =GetGridData();
    dr = dt.NewRow();
    dt.Rows.Add(TextBox1.Text, TextBox2.Text);
    GridView1.DataSource = dt;
    GridView1.DataBind();
    }

  private DataTable GetGridData ()
  {
   DataTable _datatable = new DataTable();
   for (int i = 0; i < grdReport.Columns.Count; i++)
   {
    _datatable.Columns.Add(grdReport.Columns[i].ToString());
   }
foreach (GridViewRow row in grdReport.Rows)
{
    DataRow dr = _datatable.NewRow();
    for (int j = 0; j < grdReport.Columns.Count; j++)
    {
        if (!row.Cells[j].Text.Equals("&nbsp;"))
            dr[grdReport.Columns[j].ToString()] = row.Cells[j].Text;
    }

    _datatable.Rows.Add(dr);
  }
return _dataTable;
}

You can store the values from each PostBack into a Session or ViewState , otherwise you'll lose all the data when the button is clicked again.您可以将每个PostBack的值存储到 Session 或ViewState ,否则再次单击该按钮时您将丢失所有数据。

protected void Button1_Click(object sender, EventArgs e)
{
    DataTable dt;

    //check if the datatable already exists in the viewstate, if not create a new datatable
    if (ViewState["myTable"] == null)
    {
        dt = new DataTable();
        dt.Columns.AddRange(new DataColumn[2] { new DataColumn("Name", typeof(string)), new DataColumn("Age", typeof(decimal)) });
    }
    else
    {
        //correct viewstate exists, cast back to a datatable
        dt = ViewState["myTable"] as DataTable;
    }

    //add the new values
    dt.Rows.Add(TextBox1.Text, Convert.ToDecimal(TextBox2.Text));

    //bind the datatable to the gridview
    GridView1.DataSource = dt;
    GridView1.DataBind();

    //save the datatable into the viewstate
    ViewState["myTable"] = dt;

    //clear the textboxes
    TextBox1.Text = "";
    TextBox2.Text = "";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM