none
Retrieving selected values of dataGridViewComboBoxColumn RRS feed

  • Question

  • I have a dataGridView (dgv) with a databound dataGridViewComboBoxColumn (dgvCbCol).  After making the selections on the dgvCbCol, I save the selected values to the database and close the form. When I reopen the form, I would like to see the dgvCbCol restore all the selections I have made. How do I do that? Thanks.
    Monday, April 6, 2009 12:27 AM

Answers

  • Hi Henry,

    Welcome to Visual C# General Forum.

    I'm sorry that I don't very well understand your question. Would you mind providing more information about your purpose?
    If you want to save the selected value to database and close the form, then please use the following sample code.
            public Form1()
            {
                InitializeComponent();
    
                DataGridViewComboBoxColumn dgvCbCol = new DataGridViewComboBoxColumn();
                dgvCbCol.Items.Add("1");
                dgvCbCol.Items.Add("2");
                dgvCbCol.Items.Add("3");
                dgvCbCol.Items.Add("4");
                dataGridView1.Columns.Add(dgvCbCol);
            }
    
            private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
            {
                string sValue = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
    
                // codes save the sValue to database
    this.Close(); // close the form }

    If you want to fill the combobox with the values selected from database, then use the sample code below:
            private void Form1_Load(object sender, EventArgs e)
            {
                DataTable dt = new DataTable();
    // codes get the values from database to dt; DataGridViewComboBoxColumn dgvCbCol = new DataGridViewComboBoxColumn(); for (int i = 0; i < dt.Rows.Count; i++) { dgvCbCol.Items.Add(dt.Rows[i][0].ToString()); } dataGridView1.Columns.Add(dgvCbCol); }

    Please follow up with more necessary information if you still have problem.
    By the way, questions about Windows Form controls databinding , please consider posting your thread to Windows Forms Data Controls and Databinding Forum next time. You may get quicker and better responses there.
    Thank you!

    Best regards,
    Guo


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Edited by Guo Surfer Wednesday, April 8, 2009 4:24 AM add a link
    • Marked as answer by Guo Surfer Monday, April 13, 2009 3:34 AM
    Wednesday, April 8, 2009 4:19 AM