I have 2 datagrid, 1 with details of Category and the other with the products user selected from the other datagrid, what Im saying is I have a select command on my 1st datagrid so when I select something from my 1st datagrid it should be added to the 2nd datagrid and that is workin but the problem is everytime I select somethin from the 1st datagrid adding to the 2nd datagrid it overwrites the existing record on the 2nd datagrid instead of adding on top of... can anyone help me please here the code to add what is from the 1st grid to the second one

imgItems.ImageUrl = "Pictures/" + dgItems.SelectedItem.Cells[2].Text;
            string strDetails = "sdp_ViewProducts";
            //CommandType.StoredProcedure;
            DataSet dsDetails = new DataSet();
            SqlDataAdapter daDetails = new SqlDataAdapter(strDetails, conn);
            daDetails.SelectCommand.CommandText = "Select ProductID, ProductName, UnitPrice, UnitsInStock, UnitsOnOrder from dbo.Products where ProductID = " +
                    dgItems.SelectedItem.Cells[1].Text;

            daDetails.Fill(dsDetails, "Products");
            dgDetails.DataSource = dsDetails;
            dgDetails.DataMember = "Products";
            dgDetails.DataKeyField = "ProductID";
            dgDetails.DataBind();
            dgDetails.Visible = true;

Thanx in advance

Hi...
The code you are following does overwrites the Grid.

What you can do is
- Copy the data getting from the dsDetails.Table[0] into the DataTable
- Copy that Datatable into the session
- and then merge that datatable coming from dsDetails.Table[0] with the session Datatable everytime
- then Bind the Datatable from the session into dgDetails

That could be the problem, but then can you provide me with the code similar to that, coz Im clueless what ur talking about

Thanx hey

You might want to put the datasource of datagridview2 into a viewstate and read it back after Page.IsPostBack.

That aint hard to manage....

'  general declarations
dtMemory as datatable=new datatable("YouNameIt")

'  In the PageLoad_Event() :
if page.ispostback then
   dtMemory = ctype(Viewstate("TEST"), datatable)
else
   dtMemory = tblAdapter.Fill(blablabla)
   ViewState("TEST") = dtMemory
end if

DataGridView2.datasource = dtMemory
DataGridView2.datakeynames = new string() {"Your_ID_Field_Name}
DataGridView2.databind

After the ispostback is checked you can add records to the dtMemory datatable for each field the user has selected.....

This article has been dead for over six months. Start a new discussion instead.