C#添加数据到DataGridView表格
1 public delegate void UpDataViewDelObject(object o); 2 public void UpDateView(object o) 3 { 4 try 5 { 6 object[] obj = (object[])o; 7 8 DataGridView dataGridView = (DataGridView)obj[0]; 9 int id = (int)obj[1]; 10 11 if (dataGridView.InvokeRequired) 12 { 13 UpDataViewDelObject del = new UpDataViewDelObject(UpDateView); 14 this.Invoke(del, new object[] { dataGridView, id }); 15 } 16 else 17 { 18 dataGridView.Rows.Clear(); 19 20 DataGridViewRow row = new DataGridViewRow(); 21 22 23 //添加数据 24 DataGridViewCell cell_01 = new DataGridViewTextBoxCell(); 25 cell_01.Value = id.ToString(); 26 row.Cells.Add(cell_01); 27 28 29 //显示颜色 30 row.DefaultCellStyle.ForeColor = Color.Red; 31 32 //添加到界面 33 dataGridView.Rows.Add(row); 34 35 36 } 37 } 38 catch (Exception ex) 39 { 40 } 41 }