Display records on the grid based on selected item in the combobox

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kurtzky
    New Member
    • Nov 2008
    • 26

    Display records on the grid based on selected item in the combobox

    i created a form that should function as follows:

    i will enter a number in a textbox..then it should query from the database all the records which has that number..these records will have a different item no in it..then, these records will be saved in a temporary datatable (which i made in a separate class, the name is WBASKET)...The item nos of these records will be displayed in a combobox, say item1, item2, etc..

    then,i have a datagrid in the form..what will be displayed in it are those records saved in the Temporary datatable that i made, not directly querying from the database.

    i already created a function that will let the program display on the grid the records that have the item no that i selected in the combobox...say i selected item1, then all records which has item1 in the temporary table will
    be displayed on the grid.

    //function for displaying records on the grid which has the same item no as the one selected in the combox
    //wbasket is the temporary datatable that i made.
    //Name: FillGridByItem( )

    DataTable dt = new DataTable();
    dt = wbasket.DataTab le.Clone();
    int index = 0;
    try
    {
    foreach(DataRow dr in wbasket.DataTab le.Rows)
    {
    if(dr["item_no"].ToString().Tri m() == cmbItems.Select edItem.ToString ().Trim())
    {
    DataRow dr2 = dt.NewRow();
    int i = 0;
    foreach(DataCol umn dc in wbasket.DataTab le.Columns)
    {
    dr2[i] = dr[dc.ColumnName];
    i++;
    }
    dt.Rows.Add(dr2 );
    }
    }
    dgrWarehouse.Da taSource = null;
    dgrWarehouse.Da taSource = dt;
    }

    after displaying all those records in the datagrid, once i click a record, then these data will be displayed in their corresponding textboxes...say i have a textbox for item name..then the item name field from my selected index in the grid will be displayed...(st ill the selected item in the combobox is the same as the item no that i clicked on the grid..)..up to there it is working fine.

    the problem is this..say i have two items in the combobox. i selected the second item, example item2..then the program will display all records with item2..it will display the data to their corresponding textboxes, but when i click
    the save button to update the temporary datable that i made,what is being updated is the first index, which is item1, not item2..

    //UpdateGrid() function --being called when i click the save button

    DataTable dt = (DataTable)(dgr 1.DataSource);
    if(dt.Rows.Coun t > 0)
    {
    if (dgrWarehouse.I sCellSelected)
    {
    DataRow dr = wbasket.DataTab le.Rows[dgr1.CurrentRow Index];
    dr["quantity"]= txtQuantity.Tex t;
    dr["name"] = txtName.Text
    wbasket.DataTab le.AcceptChange s();
    FillGridByItem( );
    }

    i hope you can help me with this...thanks..
  • kurtzky
    New Member
    • Nov 2008
    • 26

    #2
    i know the error occurs in DataRow dr = wbasket.DataTab le.Rows[dgrWarehouse.Cu rrentRowIndex]; under the UpdateGrid function, because the currentRowIndex of the grid is different from the wbasket's index, since i fill the grid by item no (FillGridByItem function)..

    say i have displayed one item in the grid, which is item2..since i filtered it first based on the selected item in the combobox, item2 becomes the first index in the grid..but in the temporary datatable, the row number of item2 is 2...

    but i don't know how to correct it.. :(

    Comment

    • kurtzky
      New Member
      • Nov 2008
      • 26

      #3
      got it...it works fine now...:)

      Comment

      Working...