1

I am creating a Desktop Application and I have a grid named custCartGrid on my form. I want to select Transaction type like Sale or Return through combo-box in row in Grid. I am new to combo-boxes in Grid so I have not much idea that why I am not getting the required results.Here is the code that I have applied to get combo-box.

DataGridViewComboBoxColumn dcom = new DataGridViewComboBoxColumn();
        dcom.HeaderText = "Combobox";
        dcom.Items.Add("Sale");
        dcom.Items.Add("Return");
        custCartGrid.Columns.Add(dcom);
  1. When I am inserting second row, it adds another combo-box in first row like I have 2 combo-boxes in first row and then it creates second row with two combo-boxes. Like wise for third row.
  2. I am not getting items in combo-box that I have added through items.add method.

Please help me in this..

this is the complete code that i used for inserting values in grid

DateTime dt = DateTime.Now;
        string date = dt.ToShortDateString();
        //date
       // determineTransactionType();

        custCartGrid.Rows.Add();
        GridRow = custCartGrid.Rows.Count - 1;

        custCartGrid["CODE", GridRow].Value = productDetails.Tables[0].Rows[0]["ProductID"].ToString();


        custCartGrid["Name", GridRow].Value = productDetails.Tables[0].Rows[0]["ProductName"].ToString();

        custCartGrid["PRICE", GridRow].Value = tb_FP_Price_Single_Product.Text.Trim();
        custCartGrid["CATEGORY", GridRow].Value = productDetails.Tables[0].Rows[0]["CatName"].ToString();

        custCartGrid["MODE", GridRow].Value = "Sale";

        DataGridViewComboBoxColumn dcom = new DataGridViewComboBoxColumn();
        dcom.HeaderText = "Combobox";
        dcom.Items.Add("Sale");
        dcom.Items.Add("Return");
        custCartGrid.Columns.Add(dcom);

this is the screenshot of form enter image description here

  • Have you changed the column count of datagridView? where is it being initilized? Post the whole code please. – usamazf Mar 21 '16 at 04:42
  • I don't see any issue with your code, include custCartGrid construction and event in the question. It might help. – Hari Prasad Mar 21 '16 at 04:42
  • @UsamaZafar i dont know much about that, would you please change my code to my condition please?i want combobox in my row – Iqra Qureshi Mar 21 '16 at 04:45
  • The code you have posted seems to be fine. I can't be sure about the issue before seeing the whole thing. Post the portion of code used to construct 'custCartGrid' – usamazf Mar 21 '16 at 04:47
  • @UsamaZafar i have edited the question. please check it – Iqra Qureshi Mar 21 '16 at 04:57
  • @IqraQureshi Rest of the Grid Data is appearing fine? – usamazf Mar 21 '16 at 05:04
  • @UsamaZafar yes i am getting previous values fine in grid, just combobox section is not working – Iqra Qureshi Mar 21 '16 at 05:07
  • is it possible for you to suggest me the easiest way of doing that? – Iqra Qureshi Mar 21 '16 at 05:08
  • When you add a third row, are you seeing a third `DataGridViewComboBoxColumn`? It sounds like the code you posted is being called *for each row* you are adding. This is acceptable for adding a row and its data, but not for adding the `DataGridViewComboBoxColumn` - that should be added *once*, **before** the rows are added. – OhBeWise Mar 21 '16 at 14:43

1 Answers1

0

Try the following modified code to add the ComboBoxColum into your GridView:

    DataGridViewComboBoxColumn dcom = new DataGridViewComboBoxColumn();
    dcom.HeaderText = "Combobox";
    dcom.Name = "cmb";
    dcom.MaxDropDownItems = 2;
    dcom.Items.Add("Sale");
    dcom.Items.Add("Return");
    custCartGrid.Columns.Add(dcom);
usamazf
  • 3,195
  • 4
  • 22
  • 40