0
votes

I have a dataGridView and one of its column type is ComboBox. That comboBox is bind with a dataSource. the problem is, when i set value to this comboBox using the following line:

for (int i = 0; i < lstData.Count; i++)
{
   grd["ColumnName",i].Value = lstData[i].Data;
}

or using following line:

grd.Rows.Add(lstData[i].Data)

it should Show Short_Description (0 - Reg) instead of Report_Code (0). but it set the value as Text to dropdown instead of setting the Value. What i am missing in this? Screenshots are attached for reference.

enter image description here enter image description here

1
check your reportCodeLookupBinding Object value. it may set 0 for short_description also report_CodeBaskar John
you question is not clear, the problem to setup the combo box value? or combo box text display?Baskar John
@BaskarJohn problem is to set the value to comboBoxMuhammad Muneeb Hafeez
'lstData[i].Data' what is the type? custom object or array or data value?Baskar John
this is a list of dataMuhammad Muneeb Hafeez

1 Answers

0
votes
public partial class GridManualBind : Form
{
    //Binding Source for datagridView
    BindingSource datasource = new BindingSource();

    //Binding Source for datagridView Combobox Column
    BindingSource reportLookupBinding = new BindingSource();
    List<ManualData> list = new List<ManualData>();
    public GridManualBind()
    {
        InitializeComponent();
        dataGridView1.AutoGenerateColumns = false;
        datasource.DataSource = typeof(ManualData);
        reportLookupBinding.DataSource = typeof(ManualDataReport);

        dataGridView1.DataSource = datasource;

        // column
        dataGridView1.Columns.Add(new DataGridViewTextBoxColumn
        {
            HeaderText = "Price Type",
            DataPropertyName = "PriceType"
        });

        // As you see in the picture its configured for combo box column
        dataGridView1.Columns.Add(new DataGridViewComboBoxColumn
        {
            HeaderText = "Report Code Lookup",
            DataPropertyName = "ReportCodeLookup",
            DataSource = reportLookupBinding,
            DisplayMember = "Short_Description",
            ValueMember = "Report_Code"
        });

        //Adding Data for Combo Box data Source
        reportLookupBinding.Add(new ManualDataReport { Report_Code = 0, Short_Description = "0-Reg" });
        reportLookupBinding.Add(new ManualDataReport { Report_Code = 1, Short_Description = "1-Post" });

        list.AddRange(new ManualData[]
        {
             new ManualData { PriceType="Reg", ReportCodeLookup=0 },
             new ManualData { PriceType="Post", ReportCodeLookup=1 }
        });
        //Adding Data for grid View data Source
        datasource.Add(list[0]);
        datasource.Add(list[1]);

    }
    private void button1_Click(object sender, EventArgs e)
    {
        // you can Set the combo box value using this;
        list[0].ReportCodeLookup = 1;
        dataGridView1.Invalidate();
    }

    private class ManualData
    {
        public string PriceType { get; set; }
        public int ReportCodeLookup { get; set; }
    }

    private class ManualDataReport
    {
        public int Report_Code { get; set; }
        public string Short_Description { get; set; }
    }
}

to set the value for the combo box cell value

    private void button1_Click(object sender, EventArgs e)
    {
        // you can Set the combo box value using this;
        list[0].ReportCodeLookup = 1; // depend on combo box cell value
        dataGridView1.Invalidate();
    }