C#Winform中DataGridView往某一行新增数据
//添加按钮列
private void AddBtnColumn()
{
DataGridViewButtonColumn colBtn = new DataGridViewButtonColumn();
colBtn.UseColumnTextForButtonValue = true;
colBtn.Text = "选中";
dataGridView1.Columns.Add(colBtn);
}
//新增事件
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex.Equals(1))//定位到按钮列
{
MessageBox.Show("选中的值为:"+dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString());
}
}
代码如下:
DATATABLE dt=new DATATABLE();
DATAGRIDEVIEW.DATASOURCE=dt
DATAROW myrow=dt.NEWROW();
myrow[0]="aaa";
dt.ROWS.ADD(myrow);
DATAGRIDEVIEW.Refresh();