none
DataGridComboBoxCell always shows the first item in the list RRS feed

  • Question

  • hi Team,

    I am having a weird issue here, whereby i have a datagridview has 9 columns, the 2nd and 3rd columns are DataGridViewComboBoxColumn, and the last 3 columns are DataGridViewButtonColumn, now the 2nd DataGridViewComboBoxColumn has about 8 items in the collection, as per below image:

    in the 3rd DataGridViewComboBoxColumn, there are about 3 items in the collection, as per below:


    now the DataGridView got populated from a file, and the funny thing was the 3rd DataGridViewComboBoxColumn does populated the correct value into the cell, but the 2nd DataGridViewComboBoxColumn always shows the first item in the collection, as per below: 

    although by debugging i can confirm the correct value has been assigned to the 2nd DataGridViewComboBoxColumn.

    The is the file that got populated into that DataGridView:

     this is the code that populate that DataGridView:

    private void Form1_Load(object sender, EventArgs e)
    {
    	ArrayList alKB = new ArrayList();
    	alKB = LoadKB(@"C:\temp\file.txt");
    	LoadSql(alKB);
    	alKB.Clear();
    }
    
    private ArrayList LoadKB(string kbPath)
    {
    	ArrayList alKB = new ArrayList();
    	try
    	{
    		if (!File.Exists(kbPath))
    		{
    			var file = File.Create(kbPath);
    			file.Close();
    		}
    		using (StreamReader sr = new StreamReader(kbPath))
    		{
    			string value = string.Empty;
    			while (!string.IsNullOrEmpty(value = sr.ReadLine()))
    			{
    				alKB.Add(value);
    			}
    		}
    	}
    	catch (Exception x)
    	{
    		MessageBox.Show(x.Message);
    	}
    	return alKB;
    }
    
    private void LoadSql(ArrayList alKB)
    {
    	try
    	{
    		if (alKB.Count > 0)
    		{
    			if (dtSQL.Columns.Count == 0)
    			{
    				foreach (DataGridViewColumn dc in dataGridView1.Columns)
    				{
    					dtSQL.Columns.Add(dc.Name);
    				}
    			}
    			dtSQL.Rows.Clear();
    			dataGridView1.Rows.Clear();
    			dataGridView1.Rows.Add(alKB.Count);
    			for (int i = 0; i < alKB.Count; i++)
    			{
    				string[] temp = alKB.ToArray()[i].ToString().Split('|');
    				dataGridView1.Rows[i].Cells[0].Value = temp[0];
    				dataGridView1.Rows[i].Cells[1].Value = temp[1];
    				dataGridView1.Rows[i].Cells[2].Value = temp[2];
    				dataGridView1.Rows[i].Cells[3].Value = temp[3];
    				dataGridView1.Rows[i].Cells[4].Value = temp[4];
    				dataGridView1.Rows[i].Cells[5].Value = temp[5];
    				dataGridView1.Rows[i].Cells[6].Value = "Edit";
    				dataGridView1.Rows[i].Cells[7].Value = "Save";
    				dataGridView1.Rows[i].Cells[8].Value = "Cancel";
    			}
    
    			for (int i = 0; i < dataGridView1.Rows.Count; i++)
    			{
    				dtSQL.Rows.Add(dataGridView1.Rows[i]);
    				dtSQL.Rows[i][0] = dataGridView1.Rows[i].Cells[0].Value;
    				dtSQL.Rows[i][1] = dataGridView1.Rows[i].Cells[1].Value;
    				dtSQL.Rows[i][2] = dataGridView1.Rows[i].Cells[2].Value;
    				dtSQL.Rows[i][3] = dataGridView1.Rows[i].Cells[3].Value;
    				dtSQL.Rows[i][4] = dataGridView1.Rows[i].Cells[4].Value;
    				dtSQL.Rows[i][5] = dataGridView1.Rows[i].Cells[5].Value;
    				dtSQL.Rows[i][6] = dataGridView1.Rows[i].Cells[6].Value;
    				dtSQL.Rows[i][7] = dataGridView1.Rows[i].Cells[7].Value;
    				dtSQL.Rows[i][8] = dataGridView1.Rows[i].Cells[8].Value;
    			}
    		}
    	}
    	catch (Exception x)
    	{
    		MessageBox.Show(x.Message);
    	}
    }
    
    private void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e)
    {
    
    }

    Any help would be greatly appreciated.

    Thursday, November 22, 2018 12:56 AM

Answers

  • Hi,

    >> … why would adding a space solve such issue …

    We can see that there is a space between the item and the number in the collection of Database:

    So the item in file.txt can not match it due to the absence of a space.

    >> and in real word, it only has one word

    For this, we have a collection with "one-word" items:

    The file.txt:

    Result:

    Regards,

    Stanly


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by AndieDu Thursday, November 22, 2018 6:56 AM
    Thursday, November 22, 2018 6:15 AM
    Moderator
  • hi Stanly,

    The issue has been found and thanks to point me to the right direction.

    the data in the file that used to populate the 2nd DataGridViewComboBoxColumn of DataViewGrid were all upper case, whereas the items in the 2nd DataGridViewComboBoxColumn were all lower case, and i have changed the items in 2nd DataGridViewComboBoxColumn to all upper case, and now it is working as expected.

    • Marked as answer by AndieDu Thursday, November 22, 2018 6:56 AM
    Thursday, November 22, 2018 6:56 AM

All replies

  • Hi,

    >> … ut the 2nd DataGridViewComboBoxColumn always shows the first item in the collection …

    The cause of this problem is the absence of a space in "item1" within file.txt. This problem can be solved by adding a space to each item like:

    Change "item1" to "item 1":

    So the new result:

    Regards,

    Stanly


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, November 22, 2018 2:46 AM
    Moderator
  • thanks Stanly.

    not sure that's the issue though.

    the listed items in above were just an example, and in real word, it only has one word.

    btw, why would adding a space solve such issue.

    Thursday, November 22, 2018 4:00 AM
  • Hi,

    >> … why would adding a space solve such issue …

    We can see that there is a space between the item and the number in the collection of Database:

    So the item in file.txt can not match it due to the absence of a space.

    >> and in real word, it only has one word

    For this, we have a collection with "one-word" items:

    The file.txt:

    Result:

    Regards,

    Stanly


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by AndieDu Thursday, November 22, 2018 6:56 AM
    Thursday, November 22, 2018 6:15 AM
    Moderator
  • hi Stanly,

    The issue has been found and thanks to point me to the right direction.

    the data in the file that used to populate the 2nd DataGridViewComboBoxColumn of DataViewGrid were all upper case, whereas the items in the 2nd DataGridViewComboBoxColumn were all lower case, and i have changed the items in 2nd DataGridViewComboBoxColumn to all upper case, and now it is working as expected.

    • Marked as answer by AndieDu Thursday, November 22, 2018 6:56 AM
    Thursday, November 22, 2018 6:56 AM