DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   ASP.NET (http://www.daniweb.com/forums/forum18.html)
-   -   Problem with DataGrid (http://www.daniweb.com/forums/thread160551.html)

Traicey Dec 3rd, 2008 9:43 am
Problem with DataGrid
 
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

vizy Dec 3rd, 2008 12:57 pm
Re: Problem with DataGrid
 
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

Traicey Dec 4th, 2008 2:47 am
Re: Problem with DataGrid
 
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

4advanced Dec 4th, 2008 1:51 pm
Re: Problem with DataGrid
 
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.....


All times are GMT -4. The time now is 8:51 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC