Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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 at 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 at 31-Jan-13 7:45am
   
Declare the Datatable in global scope.
Wasim1989 at 31-Jan-13 7:54am
   
ok thanx
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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  
v2
Comments
CodeMaster_Noob at 31-Jan-13 7:31am
   
ViewState is in Asp.net... you can't use it in windows application.

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

  Print Answers RSS
0 Maciej Los 200
1 Kornfeld Eliyahu Peter 175
2 BillWoodruff 160
3 George Jonsson 140
4 OriginalGriff 126


Advertise | Privacy | Mobile
Web01 | 2.8.141015.1 | Last Updated 31 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100