如果想固定列名的话可以直接在属性里传入
另一种方式就是代码添加,ErroeText里面的变量名就是列名。建议用BindingSource数据绑定。这样后面改动里面的数据会随时更新
public class ErroeText
{
public int 序号 { get; set; }
public string 设备编号 { get; set; }
public string 详细错误 { get; set; }
}
List<ErroeText> erroeTexts = new List<ErroeText>();
public BindingSource binding = new BindingSource();
binding.DataSource = erroeTexts;
dataGridView1.DataSource = binding;
dataGridView1.Columns[0].Width = 60;
dataGridView1.Columns[1].Width = 100;
binding.Clear();
for (int i = 0; i < UiForm.dateMsg.Count; i++)
{
//MessageBox.Show(UiForm.dateMsg[0].ToString());
binding.Add(new ErroeText { 序号 = i, 设备编号 = UiForm.dateMsg[i.ToString()].sbbh, 详细错误 = UiForm.dateMsg[i.ToString()].ccxx });
}
列宽的话一个就是上图代码中的width直接定义长度,另一个就是下方这个属性设置的是百分比
行内容的话我还是dataGridView1.Rows[0].Cells[1].Value
点击某一行的话就类似下面这样用行索引
private void dataGridView1_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
{
int i = e.RowIndex;
if (i > -1)
{
string xh = dataGridView1.Rows[i].Cells[0].Value.ToString();
Form1 formMain = new Form1();
//MessageBox.Show(dataGridView1.Rows[i].Cells[0].Value.ToString());
formMain.Show();
formMain.AddMessage(UiForm.dateMsg[xh]);
}
}