如何将DataGridView中的最后一行设置为不同的颜色
本文关键字:设置 一行 颜色 DataGridView 最后 | 更新日期: 2023-09-27 18:29:26
我已经找到了很多方法来更改DataGridView中的背景颜色,但找不到更改最后一行(条目或添加行)颜色的方法。下面是我的添加联系人按钮代码。当我改变颜色时,它会改变整个表格。我只想更改最后一行并将其选中,这样用户就可以简单地开始键入以添加新联系人。
任何帮助都将不胜感激。
private void button_Add_Contact_Click(object sender, EventArgs e)
{
customer_Ship_ContactsDataGridView.AllowUserToAddRows = true;
customer_Ship_ContactsDataGridView.ReadOnly = false;
int rowCount = customer_Ship_ContactsDataGridView.Rows.Count;
customer_Ship_ContactsDataGridView.CurrentCell = customer_Ship_ContactsDataGridView.Rows[rowCount - 1].Cells[0];
foreach (DataGridViewRow row in customer_Ship_ContactsDataGridView.Rows)
{
row.DefaultCellStyle.BackColor = Color.Yellow;
}
您在评论中提到,您希望最后一行填充一种颜色,在编辑时颜色也应该在那里,而且您希望这些事情在通过数据集检索数据时发生。您还希望在加载表单时关注最后一个可编辑行。。.
试着在一个单独的项目文件中使用代码,这样你就可以看到它是如何工作的。同时创建一个空项目,只添加一个dataGridView1。
public partial class Form1 : Form
{
DataSet dSet = new DataSet();
public Form1()
{
InitializeComponent();
this.Shown += Form1_Shown;
dataGridView1.AllowUserToAddRows = false;
dataGridView1.CellBeginEdit += dataGridView1_CellBeginEdit;
dataGridView1.RowStateChanged += dataGridView1_RowStateChanged;
dSet = fillDataSet();
dataGridView1.DataSource = dSet.Tables[0].DefaultView;
dSet.Tables[0].Rows.Add();
}
void Form1_Shown(object sender, EventArgs e)
{
dataGridView1.CurrentCell = dataGridView1[0, dataGridView1.Rows.Count - 1];
dataGridView1.BeginEdit(false);
}
DataSet fillDataSet()
{
DataSet dSet = new DataSet();
dSet = new DataSet();
DataTable table = new DataTable("Names");
table.Columns.Add("ID");
table.Columns.Add("Name");
table.Columns.Add("Gender");
table.Rows.Add(new object[] { 1, "Salim", "Male" });
table.Rows.Add(new object[] { 2, "Salim", "Male" });
table.Rows.Add(new object[] { 3, "Salim", "Male" });
table.Rows.Add(new object[] { 4, "Salim", "Male" });
table.Rows.Add(new object[] { 5, "Salim", "Male" });
table.Rows.Add(new object[] { 6, "Salim", "Male" });
table.Rows.Add(new object[] { 7, "Salim", "Male" });
table.Rows.Add(new object[] { 8, "Salim", "Male" });
dSet.Tables.Add(table);
return dSet;
}
void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
if (e.ColumnIndex == dSet.Tables[0].Columns.Count - 1)dSet.Tables[0].Rows.Add();
}
private void dataGridView1_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e)
{
try
{
String ssNair = e.StateChanged.ToString();
if (e.Row.Index > 0)
dataGridView1.Rows[e.Row.Index - 1].DefaultCellStyle.BackColor = Color.White;
dataGridView1.Rows[e.Row.Index].DefaultCellStyle.BackColor = Color.Yellow;
}
catch { }
}
}
对于所需的选项类型,应将AllowUsersToAddRows属性设置为false,并根据某些条件添加行。
在这里,我添加了一个新行,同时正在编辑最后一行的最后一列。
由于绑定了dataSet,dataGridView1.rows.Add()无法直接添加行;因此,这里的行是使用DataTable.rows.Add()方法添加的。
而且DataGridView.RowsAdded事件在用Dataset填充数据时不起作用,因此使用了DataGridView_RowStateChanged事件
为什么不使用For循环而不是foreach;
int counter = customer_Ship_ContactsDataGridView.Rows.count;
for(int i = 0; i< counter; i++){
if(i == counter){
//this is where your LAST LINE code goes
row.DefaultCellStyle.BackColor = Color.Yellow;
} else {
//this is your normal code NOT LAST LINE
row.DefaultCellStyle.BackColor = Color.Red;
}
}
所以基本上你的for循环会在每一行迭代一次。在最后一次迭代中,它将执行备用代码。
值得注意的是:rows.count实际上可能是rows.length或其他一些措辞,为了将行数放入该变量中,我只是想提请您注意作为解决方案的for循环。
重新编辑:上面的示例使所有行变黄,最后一行变红
这里有一个简单而有效的。。创建行添加事件
public Form1()
{
dataGridView1.RowsAdded += dataGridView1_RowsAdded;
}
void dataGridView1_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
if (e.RowIndex > 0)
dataGridView1.Rows[e.RowIndex - 1].DefaultCellStyle.BackColor = Color.White;
dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Yellow;
}