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.