1 如何自定义列名和列宽:
System.Windows.Forms.DataGridTextBoxColumn dataGridTextBoxColumn1;
DataGridTableStyle dataGridTableStyle1 = new System.Windows.Forms.DataGridTableStyle();
for (int x = 0; x < RecvTable.Columns.Count; x++)
{
dataGridTextBoxColumn1 = new System.Windows.Forms.DataGridTextBoxColumn();
dataGridTextBoxColumn1.HeaderText = RecvTable.Columns[x].ColumnName;
dataGridTextBoxColumn1.MappingName = RecvTable.Columns[x].ColumnName;
dataGridTextBoxColumn1.Width = 100;
dataGridTableStyle1.GridColumnStyles.Add(dataGridTextBoxColumn1);
}
this.dgView.TableStyles.Add(dataGridTableStyle1);
dgView.DataSource = RecvTable.DefaultView;
dgView.TableStyles[0].MappingName = RecvTable.TableName;
2、设置选中状态
private void checkBox1_CheckStateChanged(object sender, EventArgs e)
{
try
{
if (checkBox1.Checked)
{
for (int x = 0; x < RecvTable.Rows.Count; x++)
{
dgView[x, 0] = "■";
}
}
else
{
for (int x = 0; x < RecvTable.Rows.Count; x++)
{
dgView[x, 0] = "□";
}
}
}
catch
{ }
}
private void dgView_Click(object sender, EventArgs e)
{
if (dgView.CurrentRowIndex < 0)
return;
dgView.Select(dgView.CurrentRowIndex);
if (dgView[dgView.CurrentRowIndex, 0].ToString().Trim() == "□")
{
dgView[dgView.CurrentRowIndex, 0] = "■";
}
else
{
dgView[dgView.CurrentRowIndex, 0] = "□";
}
}