ListBox and ComboBox are very useful control in developing window application. These two controls are very closely related to each other but they are quite different in some manner. ListBox is collection of data item which can be selected single or multiple at a time and the data item is called listbox item where as ComboBox data item can be selected one at a time. Here I am focusing on ListBox events which are widely used in an application. ListBox have many events but in selected index change event are widely used. So let’s see the brief demonstration on this event.
Step 1: First open visual studio and select new project from file menu and then select project (if you want to create window application) or website (if you want to create web application). Here I am choosing window application.
Step 2: After creating successful new project drag listbox from left pane toolbox and drop it into window form.
Here I am taking two LisBox one for select category and second to display appropriate information corresponding to select category name and one datagridview to explore information .
For creating this write the following code into select index change method. Let’s brief demonstration of ListBox.
private voidlist_CollectionItem_SelectedIndexChanged(objectsender, EventArgse)
{list_ShowItem.Items.Clear();
//// create the object of listbox select items collection
ListBox.SelectedObjectCollectionlst_data=list_CollectionItem.SelectedItems;
foreach (objectdatainlst_data)
{ /// pass items name to databind method to load bind item into second list box
DataBind(data.ToString());
}
} public voidDataBind(stringcheck)
{ /// bind data if class item is selected
if(check=="Class")
{ list_ShowItem.Items.Insert(0, "--------Class--------");
list_ShowItem.Items.Insert(1, "7");
list_ShowItem.Items.Insert(2, "8");
list_ShowItem.Items.Insert(3, "9");
list_ShowItem.Items.Insert(4, "10");
list_ShowItem.Items.Insert(5, "11");
list_ShowItem.Items.Insert(6, "12");
} //// bind data if subject is slelected
else if(check=="Subject")
{ list_ShowItem.Items.Insert(0, "--------Subject--------");
list_ShowItem.Items.Insert(1, "Math");
list_ShowItem.Items.Insert(2, "English");
list_ShowItem.Items.Insert(3, "Science");
list_ShowItem.Items.Insert(4, "English");
list_ShowItem.Items.Insert(5, "History");
list_ShowItem.Items.Insert(6, "Civics");
}
//// bind data if stream is selected
else
{ list_ShowItem.Items.Insert(0, "--------Stream--------");
list_ShowItem.Items.Insert(1, "Science");
list_ShowItem.Items.Insert(2, "Art");
}
}
private void list_ShowItem_SelectedIndexChanged(objectsender, EventArgse)
{dataGridView1.Visible= true;
string checkinput=list_ShowItem.Text;
try
{
int convertval= Convert.ToInt32(checkinput); /// this line genrate exception exception if selected items not class item
//// explore information of apprpriate class selected
SqlConnection con= new SqlConnection(ConnectionString.ConnString());
con.Open();
SqlDataAdapter da= new SqlDataAdapter();
da.SelectCommand=new SqlCommand("select * from tblLOGIN where userid in (select stu_id from tblSTUDENT where STU_CLASS ='"+list_ShowItem.Text+"')", con);
DataSet ds= newDataSet();
da.Fill(ds);
if (ds.Tables[0].Rows.Count>0)
{ ///// bind data set with datagridview
dataGridView1.DataSource=ds.Tables[0];
} else
{ //// if record not available then this notification is generated
dataGridView1.Visible=false;
MessageBox.Show("No Records found");
}
} catch (Exception ae)
{ //// data bind for stream category item
if(checkinput=="Art"||checkinput=="Science")
{ SqlConnection con= new SqlConnection(ConnectionString.ConnString());
con.Open();
SqlDataAdapter da= new SqlDataAdapter();
da.SelectCommand=new SqlCommand("select * from tblLOGIN where userid in (select stu_id from tblSTUDENT where stream ='"+list_ShowItem.Text+"')", con);
DataSet ds= newDataSet();
da.Fill(ds);
if (ds.Tables[0].Rows.Count>0)
{ dataGridView1.DataSource=ds.Tables[0];
} else
{ /////
dataGridView1.Visible=false;
MessageBox.Show("No Records found");
}
} else if (checkinput== "--------Class--------"|| checkinput== "--------Subject--------"||checkinput== "--------Stream--------")
{ dataGridView1.Visible=false;
} //// bind data item with gridview for subject category
else
{ SqlConnection con= new SqlConnection(ConnectionString.ConnString());
con.Open();
SqlDataAdapter da= new SqlDataAdapter();
da.SelectCommand=new SqlCommand("select * from tblSUBJECTT where SUB_NAME+ list_ShowItem.Text+ ='""'", con);
DataSet ds= newDataSet();
da.Fill(ds);
if (ds.Tables[0].Rows.Count>0)
{ //// bind dataset with datagridview
dataGridView1.DataSource=ds.Tables[0];
} else
{ /// show notification if record not found
dataGridView1.Visible=false;
MessageBox.Show("No Records found");
}
}
}
} }
If select multi- data item then all information are displayed into second listbox.
And if want to select any information from second listbox then its information displayed into datagridview.
Leave Comment
1 Comments
View All Comments