I have created my own dataset: this.abcTableAdapter.Fill(this.dataSet1.abc);

And I created DataGridViewComboBoxColumn called AllGrades:

DataGridViewComboBoxColumn AllGrades = new DataGridViewComboBoxColumn();            
        for (int grade = 5; grade <= 10; grade++)
        {
            AllGrades.Items.Add(grade);
        }
        AllGrades.DataPropertyName = "Grade";
        AllGrades.HeaderText = "Grade";
        AllGrades.Width = 60;
        AllGrades.MaxDropDownItems = 6;
        AllGrades.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;
        dataGridView1.Columns.Add(AllGrades);

In a dataGridView I have a column Grades, which is connected to a database. I have created this code above in a case, if its needed to repair the grade. Thats why I decided to use a dropDown menu. The code above shows in a dropDown menu grades from 5 to 10.
I did aditional button Save, which will save the new selected value into db. But I am struggling to create additional code for inserting the new selected grade back into database.
Can someone help me out how to do it?

Recommended Answers

All 4 Replies

There is a good example at:
http://support.microsoft.com/kb/308507

If this is a clean Select statement to get the data from the databaes, then this works (SqlCommandBuilder). If your data comes from multiple joins you may want to manually post the data back using your own T-SQL code, or a stored procedure.
To find out what has changed in your datagridview (underlying datatable), you can use the underlying datatable's GetChanges method:

DataTable changes = XYZ.GetChanges(DataRowState.Modified);

Iterate through the changes and post the data back to the database.

// Jerry

I have created my own dataset: this.abcTableAdapter.Fill(this.dataSet1.abc);

And I created DataGridViewComboBoxColumn called AllGrades: DataGridViewComboBoxColumn AllGrades = new DataGridViewComboBoxColumn();
for (int grade = 5; grade <= 10; grade++)
{
AllGrades.Items.Add(grade);
}
AllGrades.DataPropertyName = "Grade";
AllGrades.HeaderText = "Grade";
AllGrades.Width = 60;
AllGrades.MaxDropDownItems = 6;
AllGrades.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;
dataGridView1.Columns.Add(AllGrades);

In a dataGridView I have a column Grades, which is connected to a database. I have created this code above in a case, if it`s needed to repair the grade. That`s why I decided to use a dropDown menu. The code above shows in a dropDown menu grades from 5 to 10.
I did aditional button Save, which will save the new selected value into db. But I am struggling to create additional code for inserting the new selected grade back into database.
Can someone help me out how to do it?

Dear, Following code might help to you to use selected grade in you update or insert query.

private void Form1_Load(object sender, EventArgs e)
        {
            DataGridViewComboBoxColumn AllGrades = new DataGridViewComboBoxColumn();
            for (int grade = 5; grade <= 10; grade++)
            {
                AllGrades.Items.Add(grade);
            }
            DataTable dt = new DataTable();
            dt.Columns.Add("Grade", typeof(int));
            dt.Rows.Add(5);
            dt.Rows.Add(6);
           
            AllGrades.DataPropertyName = "Grade";
            AllGrades.HeaderText = "Grade";
            AllGrades.Width = 60;
            AllGrades.MaxDropDownItems = 6;
            AllGrades.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;
            dataGridView1.Columns.Add(AllGrades);
            dataGridView1.DataSource = dt;
        }

        private void button1_Click(object sender, EventArgs e)
        {
          // Reference of 1st Cell's control in a selected row
            DataGridViewCell c = (DataGridViewCell)dataGridView1.SelectedRows[0].Cells[0];
            // Value at selected cell
            MessageBox.Show(c.Value.ToString());
        }
DataGridViewCell c = (DataGridViewCell)dataGridView1.SelectedRows[0].Cells[0];

And how can I fill Rows[0].Cell[0] if I dont know them? With mouse click (currentCell method) will be known which cell and row is selected.
Any idea how to work this out?

And how can I fill Rows[0].Cell[0] if I dont know them? With mouse click (currentCell method) will be known which cell and row is selected.
Any idea how to work this out?

DataGridViewCell c = (DataGridViewCell)dataGridView1.SelectedRows[0].Cells[0]; 

c.Value="New Value";
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.