C#datagridview最后一行 [英] C# datagridview last row

查看:144
本文介绍了C#datagridview最后一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨:),我将datagridview连接到sql以显示datagridview上的数据。我添加了一个复选框,并在检查时将行的颜色更改为红色。什么时候自动检查下一行。

我的问题在这里是什么时候是给我一个错误的最后一行。我想要的是什么时候是最后一行,检查并且不要尝试添加新行。



我尝试过:



使用System;

使用System.Collections.Generic;

使用System.ComponentModel;

使用System.Data;

使用System.Drawing;

使用System.Linq;

使用System.Text;

使用System.Windows.Forms;



命名空间WindowsFormsApplication1

{

公共部分类表格1:表格

{

public Form1()

{

InitializeComponent();

}



private void Form1_Load(object sender,EventArgs e)

{


//填充数据网格

this.uSR_ProdMonitorTableAdapter.FillBy(this.iso_OldDataSet.USR_ProdMonitor);



}



private void dataGridView1_CellContentClick(object sender,DataGridViewCellEventArgs e)

{

bool selected =!Convert.ToBoolean(dataGridView1.Rows [e.RowIndex] .Cells [check] .Value);

dataGridView1.Rows [1] .Selected = true;



if(selected)

{

//如果检查自动nex行

int next = this.dataGridView1.CurrentRow.Index +1;

//更改如果检查颜色为红色

dataGridView1.Rows [e.RowIndex] .DefaultCellStyle.BackColor = Color.Red;

this.dataGridView1.CurrentCell = this.dataGridView1.Rows [接下来.Cells [this.dataGridView1.CurrentCell.ColumnIndex];

}

else

{

dataGridView1 .Rows [e.RowIndex] .DefaultCellStyle.BackColor = Color.LightGreen;

dataGridView1.Rows [e.RowIndex] .Cells [check]。Value =!Convert.ToBoolean(dataGridView1.Rows [e.RowIndex] .Cells [check]。Value);



}

}





}



}

hi :) ,i connect datagridview to sql to show data on datagridview. i have add a checkbox,and change color of row to red when is check. and when is check automatic next row.
my problem is here when is the last row that give me a error. what i want is when is the last row , check and dont try to add a new row.

What I have tried:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{

//Fill Datagrid
this.uSR_ProdMonitorTableAdapter.FillBy(this.iso_OldDataSet.USR_ProdMonitor);

}

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
bool selected = !Convert.ToBoolean(dataGridView1.Rows[e.RowIndex].Cells["check"].Value);
dataGridView1.Rows[1].Selected = true;

if (selected)
{
//if is check automatic nex row
int next = this.dataGridView1.CurrentRow.Index +1;
//change color to red if check
dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Red;
this.dataGridView1.CurrentCell = this.dataGridView1.Rows[next].Cells[this.dataGridView1.CurrentCell.ColumnIndex];
}
else
{
dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.LightGreen;
dataGridView1.Rows[e.RowIndex].Cells["check"].Value = !Convert.ToBoolean(dataGridView1.Rows[e.RowIndex].Cells["check"].Value);

}
}


}

}

推荐答案

您需要检查行数而不是任意索引到下一行(可能不是存在)。

You need to check your "row count" instead of arbitrarily indexing to the "next row" (which may not exist).
int next = this.dataGridView1.CurrentRow.Index +1;



DataRowCollection.Count属性(System.Data)| Microsoft Docs [ ^ ]


谢谢格里,是的,这是正确的方法:)
thank you Gerry, yes is the correct way :)


这篇关于C#datagridview最后一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆