MattR
MattR

Reputation: 641

Add New Rows to GridView

I have a GridView on my page and on a button_OnClick event I'm wanting to add a new row to the grid.

I can add a row to the table using the following code, which works fine, but as I'm binding the DataTable to the grid any previous entries are lost.

        string selectedProduct= ddlProducts.SelectedItem.Text;
        DataTable dataTable = new DataTable();

        dataTable.Columns.Add("Product");
        DataRow dataRow;

        dataRow = dataTable.NewRow();
        dataRow["Product"] = selectedProduct;
        dataTable.Rows.Add(dataRow);

        grdSelectedProducts.DataSource = dataTable;
        grdSelectedProducts.DataBind();

So whilst I understand why this is causing the data loss, I'm not really sure how to get around it.

How can I add a new row to the GridView on each button click whilst retaining the previously added row? The data is not stored anywhere other than the grid itself, so there is no real datasource.

There are options such as Add row to gridview on client side which uses Jquery, but I have read that it isn't a great idea to use that when adding / removing items from the grid. Perhaps that is wrong? Or there is this Add new row to gridview but there isn't much detail there.

Upvotes: 0

Views: 2303

Answers (3)

// OnButten click
  function addrow(sender, args) {
       // td add as per your column required
        $("#GridView1 tbody").append('<tr><td>00001</td><td>Mr.</td><td>LOKESH N</td></tr>');
    }

Upvotes: 0

jomsk1e
jomsk1e

Reputation: 3625

Here is a sample you can try:

DataTable dt = new DataTable(); 
if (ViewState["CurrentTable"]!=null)
{
    dt =  (DataTable)ViewState["CurrentTable"];
  }

else
 {
     dt.Columns.Add(new DataColumn("Col1", typeof(string)));
    dt.Columns.Add(new DataColumn("Col2", typeof(string))); 
}

DataRow dr = null;            
dr = dt.NewRow();
 dr["Col1"] = "tes";      
dr["Col2"] = "test";
dt.Rows.Add(dr);

ViewState["CurrentTable"] = dt; 
GridView1.DataSource = dt; 
GridView1.DataBind();

Sorry for bad formatting, typed this with my cellphone. Hope it helps! :-)

Upvotes: 0

Win
Win

Reputation: 62300

You need to store the Products into ViewState (or SessionState or Database) so that it can persist on post back.

For example,

private DataTable ProductDataTable
{
    get { return ViewState["ProductDataTable"] as DataTable ?? new DataTable(); }
    set { ViewState["ProductDataTable"] = value; }
}

protected void AddRowButton_Click(object sender, EventArgs e)
{
    string selectedProduct = ddlProducts.SelectedItem.Text;

    // Get the data from ViewState
    DataTable dataTable = ProductDataTable;

    dataTable.Columns.Add("Product");
    DataRow dataRow;

    dataRow = dataTable.NewRow();
    dataRow["Product"] = selectedProduct;
    dataTable.Rows.Add(dataRow);

    // Save the data back to ViewState
    ProductDataTable = dataTable;

    grdSelectedProducts.DataSource = dataTable;
    grdSelectedProducts.DataBind();
}

Upvotes: 1

Related Questions