dataGridView.ColumnHeadersDefaultCellStyle.WrapMode = DataGridViewTriState.False; //设置列标题不换行 // 设定包括Header和所有单元格的列宽自动调整
dataGridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
dataGridView.ColumnHeadersDefaultCellStyle.WrapMode = DataGridViewTriState.False; //设置列标题不换行
// 设定不包括Header所有单元格的行高自动调整
dataGridView.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCellsExceptHeaders; //AllCells;设定包括Header和所有单元格的行高自动调整
dataGridView.Columns[i].MinimumWidth = 10;//设置最小宽度
dataGridView.Columns[i].FillWeight = 50;//设置列比例
dataGridView.Columns[i].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;//自动铺满
dataGridView.CurrentCell = dataGridView[0, 0];//设定0,0为当前单元格
dataGridView.Rows[0].Cells[0].Style.BackColor = Color.White;//设置单元格颜色
//获取数值
for (int cc = 0; cc < 22; cc++)//取得所有原始数据
{
if ((String)dataGridView.Rows[RowsAll].Cells[cc + 4].Value != null)
{
rawData[cc] = int.Parse((String)dataGridView.Rows[RowsAll].Cells[cc + 4].Value);
}
else
rawData[cc] = 0;
}
//绘制行号
private void dataGridView_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
{
System.Drawing.Rectangle rectangle = new System.Drawing.Rectangle(e.RowBounds.Location.X,
e.RowBounds.Location.Y,
dataGridView.RowHeadersWidth - 4,
e.RowBounds.Height);
TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(),
dataGridView.RowHeadersDefaultCellStyle.Font,
rectangle,
dataGridView.RowHeadersDefaultCellStyle.ForeColor,
TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
}
//限制输入数值
TextBox control; //定义输入框控件对象
int EnterFalag = 0;//输入过标志
private void dataGridView_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
int dataGridViewX = dataGridView.CurrentCellAddress.X;
int dataGridViewY = dataGridView.CurrentCellAddress.Y;
if (dataGridViewX == 0) return;
if (e.Control.GetType().BaseType.Name == "TextBox")
{
control = new TextBox();
control = (TextBox)e.Control;
if(EnterFalag == 0)
{
control.KeyPress += new KeyPressEventHandler(control_KeyPress);
EnterFalag = 1;
}
}
}
void control_KeyPress(object sender, KeyPressEventArgs e)
{
int dataGridViewX = dataGridView.CurrentCellAddress.X;
int dataGridViewY = dataGridView.CurrentCellAddress.Y;
if (dataGridViewX == 0) return;
//限制只能输入-9的数字,退格键,小数点和回车
if (((int)e.KeyChar >= 48 && (int)e.KeyChar <= 57) || e.KeyChar == 13 || e.KeyChar == 8)
{
e.Handled = false;
}
else
{
e.Handled = true;
MessageBox.Show("只能输入数字!");
}
}
————————————————
版权声明:本文为CSDN博主「科翼电子」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/gaoweiweiw/article/details/77664951