Click here to Skip to main content
Sign Up to vote bad
good
See more: C#
in the design i have one combo box.in that combo box items are there.
 

when i select items in the combo box that items to be added in data grid ivew by rows.
 

how can i do.
 
for example in the combobox items are as follows
 

combobox Rs
GS
VB
 
when i select the VB,Gs in combobox that items to be added in datagridview by rows.
 
Ouput as follows;
 

VB GS
 
how can i do.please help me.
Posted 31-Jan-13 1:03am


2 solutions

try this...
public void LoadGridView()
{
   Datatable dt = (Datatale)ViewState("CurrentTable");
   GridView.DataSource = dt;
   GridView.DataBind();
}
 
Combobox selectedindexchanged event
 
protected void combobox_SelectedIndexChanged(object sender, EventArgs e)
{
   Datatable dt = (Datatale)ViewState("CurrentTable");
 
    if(ViewState("CurrentTable")!= null)
    {
       dt = (Datatale)ViewState("CurrentTable");
    }
    else
    {
       dt = new Datattable;
    }
    
    dt.Colums.Add("ComboBoxItems");
    // add combox selected item to datatable object
    dt.Rows.Add(new object[] { combox.SelectedItem.Text });
    
    ViewState("CurrentTable") = dt;
    // call LoadGridView Funcation
    LoadGridView();
}
 
hope this will help you..
  Permalink  
Comments
CodeMaster_Noob - 31-Jan-13 7:31am
ViewState is in Asp.net... you can't use it in windows application.
rewrite as
protected void combobox_SelectedIndexChanged(object sender, EventArgs e)
{
  DataTable dt = new DataTable;
 
  dt.Columns.Add("ComboBoxItems");
  // add combox selected item to datatable object
  dt.Rows.Add(new object[] { combox.SelectedItem.Text });
  DataGridView.DataSource = dt;
}
  Permalink  
Comments
Wasim1989 - 31-Jan-13 7:41am
but when u select another item then first item u will lose. because u r creating new datatable object each time when selectedindex event fired. u are not storing existing items.
CodeMaster_Noob - 31-Jan-13 7:45am
Declare the Datatable in global scope.
Wasim1989 - 31-Jan-13 7:54am
ok thanx

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Christian Graus 453
1 Ron Beyer 316
2 Tadit Dash 243
3 OriginalGriff 236
4 samadhan_kshirsagar 173
0 Sergey Alexandrovich Kryukov 7,061
1 Prasad_Kulkarni 3,830
2 OriginalGriff 3,620
3 _Amy 3,370
4 CPallini 3,074


Advertise | Privacy | Mobile
Web02 | 2.6.130619.1 | Last Updated 31 Jan 2013
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid