2

My application consist of DataGridviewComboBoxColumn inside DataGridView. The ComboBoxColumns are getting filled from a database table (key, value pair). I am trying to set the default value for ComboBox column using DefaultValuesNeeded event but it is not working.

Following is the sample code:

e.Row.Cells["Job"] as DataGridViewComboBoxColumn).Value ="12"

But it shows 12 as value , instead of 12 it suppose to show actual text of 12 value.

For example:

enter image description here

DataGridViewComboBoxColumn dgvCbJob = new DataGridViewComboBoxColumn();
{
    dgvCbJob.HeaderText = "Job";
    hadd.Clear();
    hadd.Add("@Search", string.Empty);
    ds = ObjDAL.GetDataSetForPrc("prc_GetJobList", hadd);
    if (ds.Tables[0].Rows.Count > 0)
    {
        dgvCbJob.DataSource = ds.Tables[0];
        dgvCbJob.DisplayMember = "JobName";
        dgvCbJob.ValueMember = "JobMasterId";
    }
    dgvCbJob.DisplayIndex = 0;
    dgvCbJob.Width = 100;
    dgvCbJob.Name = "Job";
}
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
Parag Pathari
  • 281
  • 2
  • 5
  • 19

2 Answers2

1

To set default value for cell you can use either of these options:

  • Handle DefaultValuesNeeded event of grid and assign value to e.Row.Cells["Job"].Value
  • In your DataTable set the DefaultValue for the 'Job' DataColumn to desired value

In both options, the type of value which you assign should be the same type as DataType of the column.

Note: You should know e.Row.Cells["Job"] is not DataGridViewComboBoxColumn. If the column is combo box, then the cell is DataGridViewComboBoxCell.

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • Thanks for the information, but how to typecast string to datatype of (e.Row.Cells["Job"]) datagridviewcell? – Parag Pathari Jul 27 '16 at 09:35
  • You don't need a type cast. Assign it to the Value of cell, like what I did in answer in first option. Also if the data type of the column is `int` you should assign an `int` value to the value of cell. – Reza Aghaei Jul 27 '16 at 09:37
  • Tried by following piece of code e.Row.Cells["Job"] =sJob. And it says "cannot implicitly convert string to system.windows.form.datagridviewcell" as Error. Here sJob is string. – Parag Pathari Jul 27 '16 at 09:46
  • As I said, you should assign value to `Value` property of cell this way: `e.Row.Cells["Job"].Value = sJob;` – Reza Aghaei Jul 27 '16 at 09:49
  • I tried the same approach, but it display the output as shown in the question. When I assigned the value it should automatically pick the text which is not working in this case. – Parag Pathari Jul 27 '16 at 09:55
  • I can share an example, Then you can simply confirm if the example is working. – Reza Aghaei Jul 27 '16 at 09:58
  • By the way, it seems you didn't upvoteed or accepted for any of previous questions you have asked. You should know you should confirm a post is working using a clean environment and clean example, you may have some incorrect settings or something else which may result in some problem. I'll try to answer your main question about providing default value for a `DataGridViewComboBoxColumn`. So let me know if you are interested in having a working example. – Reza Aghaei Jul 27 '16 at 10:04
  • Yes I am interested. – Parag Pathari Jul 27 '16 at 10:21
  • Check the example :) – Reza Aghaei Jul 27 '16 at 10:54
  • Hey thanks, now it is working. It is due to datatype issue , I have corrected it and now it is working. Thanks alot – Parag Pathari Jul 27 '16 at 11:02
  • When a post answers your question, you can click on up-arrow near the post to vote for the post and say it was useful, and click on check-mark near the post to mark the post as the answer which solved the problem. – Reza Aghaei Jul 27 '16 at 11:04
  • I Also removed the example since we don't need it anymore and I believe the answer is more useful in current format – Reza Aghaei Jul 27 '16 at 11:08
  • Done. Please check – Parag Pathari Jul 27 '16 at 11:35
  • Thank, you did it :) – Reza Aghaei Jul 27 '16 at 11:41
1

In many instances this can be done in design mode, right click dgv, select the combobox column, default cell style, Data "nullValue"

Great for simpler situations like a simple choice A B C for the user, to default to A, if you later compare with the cell's value being A or B or C, it will work. It would be a problem if you compare with the selected index however, as it counts as a value out of the collection i believe

GabParrot
  • 41
  • 4