Question : How to add a combobox,calender to a DataGrid view column and bind it from database

Hi
i m working on window appl. In this i take a datagridview and bind it with database.data is coming from database. and update/add & delete button work fine.
suppose in table there are 4 fields  id,name,dateofjpoining and designation.
My problem is that i want to insert optional value from datagridview  for designation field.i mean that user can only select designation value that are in combobox i.e. user/admin/superadmin.
i also want that while insert dateofjoing a user can select date from datetimepicker or calender control so that it will in same format.
i also want that when a user update a record then at designation coulmn and dateof joining column combo and calender control will appear so that he can update values.


The code that i m using is below it work fine but additionally i want to add combo n calender in datagridview .

Pls help. any help will be appriciated

Thanks in advance
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
//This code is written on page load event it fills datagrid view
 
 string connectionString = "initial catalog=Everest;data source=MAIN;uid=sa;password=";
            conn = new SqlConnection(connectionString);
             sql = "SELECT * from project_seo where p_id>20";            
            da = new SqlDataAdapter(sql, conn);
            conn.Open();
            ds = new DataSet();
            SqlCommandBuilder commandBuilder = new SqlCommandBuilder(da);
            da.Fill(ds, "project_seo");
            bsource.DataSource = ds.Tables["project_seo"];
            dataGridView1.DataSource = bsource;
 
 
 
//it add/update data from database
 
 
 
try
            {
                if (MessageBox.Show("ARE U SURE TO Add/Update Records", "WARNING", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
                {
                    pictureBox1.Visible = true;
                    DataTable dt = ds.Tables["project_seo"];
                    this.dataGridView1.BindingContext[dt].EndCurrentEdit();
                    this.da.Update(dt);
                    
                    if (MessageBox.Show("Record has been Update", "WARNING", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
                    {
                        pictureBox1.Visible = false;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

Answer : How to add a combobox,calender to a DataGrid view column and bind it from database

You'll need to add columns to the datagridview manually.

Any object you can add to a combobox as a datasource can be added to a DataGridViewComboBoxColumn

As for adding a DataTimePicker (for example) to a datagridview, it's possible, though you'll have to extend
DataGridViewColumn, DataGridViewTextBoxCell, and DateTimePicker that implements IDataGridViewEditingControl
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
      // Create a new combobox column and add a few items
      DataGridViewComboBoxColumn comboColumn = new DataGridViewComboBoxColumn();
      comboColumn.Items.Add("Foo");
      comboColumn.Items.Add("Bar");
      comboColumn.Items.Add("Baz");
 
      // DataPropertyName is the field that the column gets it's information from
      comboColumn.HeaderText = "Header Text";
      comboColumn.DataPropertyName = "col1";
 
      gridValues.Columns.Add(comboColumn);
Random Solutions  
 
programming4us programming4us