locked
Grid view dropDown RRS feed

  • Question

  • hi

    I am windows form in which there is a grid view there are 4 columns in it .1 and 2nd one i have changed it to dropdown list .1 column contains Bank Name it is coming through database. 2nd column should display its branch of selected bank it should  come through data relation filter

    can you give me any eg to do so
    Tuesday, December 11, 2012 11:27 AM

Answers

  • Hi Lucky,

    Try this link.

    http://csharpdotnetfreak.blogspot.com/2012/07/cascading-combobox-in-winforms-windows-forms.html

    in the Selection_Changed of one dropdown you have to bind the other.


    -Arun

    • Proposed as answer by Lisa Zhu Friday, December 21, 2012 10:06 AM
    • Marked as answer by Lisa Zhu Monday, December 24, 2012 9:05 AM
    Wednesday, December 12, 2012 6:59 AM
  • Hi lucky,

    You should first add all the information of “Bank Name” to the first column. Then, get the “Bank Name” user selected.  And use it as search filter to get the information of  branch of selected bank. In the end, bind these data to column “branch of bank”.

    1. Get all the data of the bank name and bind it to column.

     string query = "select  BankName  from tb Bank  ";  
     SqlDataAdapter adapter = new SqlDataAdapter(query, conn);
     DataTable dt = new DataTable();
     adapter.Fill(dt);
             
        //bind data to ComboBox column
     ((DataGridViewComboBoxColumn)dataGridView1.Columns["BankName"]).DataSource = dt;
     ((DataGridViewComboBoxColumn)dataGridView1.Columns["BankName"]).DisplayMember = "BankName";    

    2.Fetch the selected bank and use it as search filter. Get the branch information. Use DataGridView.CellValueChanged event to achieve linkage effects.

    3. Bind the information to column2.

     private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
     {
        if (e.RowIndex >= 0)
        {
            //get the user selected value
            string BankName = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
           //get branch information
            string query2 = "select Branch  from tbBank where BankName=@in_BankName";
            SqlCommand command = new SqlCommand(query2, conn);
            command.Parameters.Add("in_BankName", SqlDbType.NVarChar).Value = BankName;
            SqlDataAdapter adpter2 = new SqlDataAdapter(command);
            DataTable dt2 = new DataTable();
            adpter2.Fill(dt2);
            //bind data to ComboBoxColumn -- Branch Of Bank
            ((DataGridViewComboBoxColumn)dataGridView1.Columns[e.ColumnIndex+1]).DataSource = dt2;
            ((DataGridViewComboBoxColumn)dataGridView1.Columns[e.ColumnIndex + 1]).DisplayMember = "Branch";
             }
      }

    Hope this helps.

    Regards,


    Lisa Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Lisa Zhu Monday, December 24, 2012 9:05 AM
    Friday, December 21, 2012 10:06 AM

All replies

  • Its good but my grid view contains 2 columns 1st of Bank Name as drop down  and 2nd column should fillter it means it should display selected bank Name sub branches.
    Tuesday, December 11, 2012 12:14 PM
  • Hi Lucky,

    Try this link.

    http://csharpdotnetfreak.blogspot.com/2012/07/cascading-combobox-in-winforms-windows-forms.html

    in the Selection_Changed of one dropdown you have to bind the other.


    -Arun

    • Proposed as answer by Lisa Zhu Friday, December 21, 2012 10:06 AM
    • Marked as answer by Lisa Zhu Monday, December 24, 2012 9:05 AM
    Wednesday, December 12, 2012 6:59 AM
  • I would suggest you to use xml to store bank names and it's branches. It is much faster to retreive using linq and then filter it. Use one xml to store bank names and another for there branches. Populate the 1st ddb with bank values using linq and then based on bank selection, query second xml using linq and then populate the 2nd ddb.

    Its easier and must simple.


    Mark it as helpful if so! Thanks

    Wednesday, December 12, 2012 2:44 PM
  • I have only one database table it consist of Bank Name and Bank Branches I want to use

    Data relation .if a particular bank is selected in drop down of a gridview the second dropdown of that gridview must filter all branches name and display only whose bank name is selected

    Wednesday, December 12, 2012 3:27 PM
  • Hi lucky,

    You should first add all the information of “Bank Name” to the first column. Then, get the “Bank Name” user selected.  And use it as search filter to get the information of  branch of selected bank. In the end, bind these data to column “branch of bank”.

    1. Get all the data of the bank name and bind it to column.

     string query = "select  BankName  from tb Bank  ";  
     SqlDataAdapter adapter = new SqlDataAdapter(query, conn);
     DataTable dt = new DataTable();
     adapter.Fill(dt);
             
        //bind data to ComboBox column
     ((DataGridViewComboBoxColumn)dataGridView1.Columns["BankName"]).DataSource = dt;
     ((DataGridViewComboBoxColumn)dataGridView1.Columns["BankName"]).DisplayMember = "BankName";    

    2.Fetch the selected bank and use it as search filter. Get the branch information. Use DataGridView.CellValueChanged event to achieve linkage effects.

    3. Bind the information to column2.

     private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
     {
        if (e.RowIndex >= 0)
        {
            //get the user selected value
            string BankName = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
           //get branch information
            string query2 = "select Branch  from tbBank where BankName=@in_BankName";
            SqlCommand command = new SqlCommand(query2, conn);
            command.Parameters.Add("in_BankName", SqlDbType.NVarChar).Value = BankName;
            SqlDataAdapter adpter2 = new SqlDataAdapter(command);
            DataTable dt2 = new DataTable();
            adpter2.Fill(dt2);
            //bind data to ComboBoxColumn -- Branch Of Bank
            ((DataGridViewComboBoxColumn)dataGridView1.Columns[e.ColumnIndex+1]).DataSource = dt2;
            ((DataGridViewComboBoxColumn)dataGridView1.Columns[e.ColumnIndex + 1]).DisplayMember = "Branch";
             }
      }

    Hope this helps.

    Regards,


    Lisa Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Lisa Zhu Monday, December 24, 2012 9:05 AM
    Friday, December 21, 2012 10:06 AM