C#datagridview最后一行

编程入门 行业动态 更新时间:2024-10-26 20:27:52
本文介绍了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最后一行

本文发布于:2023-11-06 16:10:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1564167.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:datagridview

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!