添加控件DataGridView后,添加行头:

界面

示例代码:
namespace dataGridViewTest
{
public partial class Form1 : Form
{
public int i = 0;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
dataGridViewRst.Rows.Clear();
}
private void buttoAdd_Click(object sender, EventArgs e)
{
//添加数据
dataGridViewRst.Rows.Add();
dataGridViewRst[0, i].Value = i + 1;
dataGridViewRst[1, i].Value = Math.Round(3.173, 2);
dataGridViewRst[2, i].Value = Math.Round(4.923, 2);
dataGridViewRst[3, i].Value = Math.Round(5.678, 2);
dataGridViewRst[4, i].Value = Math.Round(6.456, 2);
i++;
}
private void buttonClear_Click(object sender, EventArgs e)
{
//清除所有
dataGridViewRst.Rows.Clear();
i = 0;
}
private void buttonModify_Click(object sender, EventArgs e)
{
//获取总行数
int totalRowsCnt = dataGridViewRst.Rows.Count;
dataGridViewRst[3, 1].Value = Math.Round(8.173, 2);
// 获取单元格数据
double k = (double)dataGridViewRst[3, 1].Value;
//设置单元格背景色
dataGridViewRst.Rows[1].Cells[3].Style.BackColor = Color.Yellow;
//设置单元格前景色
dataGridViewRst.Rows[1].Cells[3].Style.ForeColor = Color.Green;
MessageBox.Show("xing");
//这个表达也可以
dataGridViewRst.Rows[1].Cells[3].Value = Math.Round(6.173, 2);
//禁止编辑
dataGridViewRst.Rows[1].Cells[3].ReadOnly = true;
}
private void buttonSelModify_Click(object sender, EventArgs e)
{
//修改选择中单元的数据
int R = this.dataGridViewRst.CurrentRow.Index;
int C = this.dataGridViewRst.CurrentCell.ColumnIndex;
dataGridViewRst.Rows[R].Cells[C].Value = Math.Round(0.00, 2);
}
}
}
本文介绍了在C#中如何使用dataGridView控件,并提供了添加行头的示例代码,帮助开发者理解其基本操作。
326

被折叠的 条评论
为什么被折叠?



