C# .NET - SELECT AN ITEM FROM LISTBOX and SHOW DATAGRIDVIEW

Asked By Rachelle Cauan on 20-Jan-12 06:31 AM
The user will select an ITEM from the listbox and each item corresponds to a particular info that will be accessed from the database. After selecting an item, a datagridview will show the information. Is this possible? If yes, could you help me with the code? I am really new with C# so I am asking your help.

Thanks for your reply.
Sandeep Mittal replied to Rachelle Cauan on 20-Jan-12 06:41 AM
Yes it is possible. On list box index change event write code fill gridview
Refer this post for list box index change event -> http://msdn.microsoft.com/en-us/library/system.windows.forms.listbox.selectedindexchanged.aspx
Chintan Vaghela replied to Rachelle Cauan on 20-Jan-12 06:51 AM

Hello,

 

string selectedValue = "";

          for (int i = 0; i < lstItem.Items.Count; i++)

          {

            if (lstItem.Items[i].Selected)

            {

              selectedValue = selectedValue + "," + lstItem.Items[i].Value;

            }

 

          }

          selectedValue = selectedValue.TrimEnd(',').TrimStart(',');

 

          // Here Bind Your Grid and your query like following way

          /// Select * from tableName where id in(selectedValue)

 

Hope this is helpful !

Thanks

 

 

 

 

 

dipa ahuja replied to Rachelle Cauan on 20-Jan-12 07:06 AM
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
      string selectedIte = listBox1.SelectedItem.ToString();
      string q = "select * from table1 where name='" + selectedIte + "'";
      SqlDataAdapter da = new SqlDataAdapter(q, "connString");
      DataTable dt = new DataTable();
      da.Fill(dt);
      DataGridView1.DataSource = dt;
    }
Suchit shah replied to Rachelle Cauan on 20-Jan-12 07:41 AM
You can just simple able to do it like below example one

private string GetConnectionString() { //DBConnection is the name of the connection string that was set up from the web config file return System.Configuration.ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString; } private void BindGridView(string field) { DataTable dt = new DataTable(); SqlConnection connection = new SqlConnection(GetConnectionString()); try { connection.Open(); string sqlStatement = "SELECT * FROM TableName WHERE ColumnName = @Value1"; SqlCommand sqlCmd = new SqlCommand(sqlStatement, connection); sqlCmd.Parameters.AddWithValue("@Value1", field); SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd); sqlDa.Fill(dt); if (dt.Rows.Count > 0) { GridView1.DataSource = dt; GridView1.DataBind(); } else { // NO RECORDS FOUND } } catch (System.Data.SqlClient.SqlException ex) { string msg = "Fetch Error:"; msg += ex.Message; throw new Exception(msg); } finally { connection.Close(); } } protected void Button1_Click(object sender, EventArgs e) { BindGridView(ListBox1.SelectecItem.Value); }


Here if you see the method calling BindGridView(List1.SelectecItem.Value);
then you will be able to find out that now Gridview is bind According to the Listbox Selected Value

Rachelle Cauan replied to Chintan Vaghela on 21-Jan-12 09:31 AM
Thanks for the help.
Chintan Vaghela replied to Rachelle Cauan on 23-Jan-12 08:25 AM
welcome
Rachelle Cauan replied to Suchit shah on 25-Jan-12 05:37 PM
thANKS..
Rachelle Cauan replied to dipa ahuja on 25-Jan-12 05:37 PM
TNX