于dataGridView行读取问题 我想用按钮实现读取dataGridView的行数,上一条,下一条,第一条,最后一条的功能,怎样实现呢??? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 dataGridView的行数 this.dataGridView1.Rows.Count.ToString();上一条, if (this.dataGridView1.Rows.Count > 1) { DataGridViewRow row = this.dataGridView1.Rows[this.dataGridView1.CurrentRow.Index - 1]; for (int i = 0; i < row.Cells.Count; i++) { MessageBox.Show(row.Cells[i].Value.ToString()); } }下一条, if (this.dataGridView1.Rows.Count > 1) { DataGridViewRow row = this.dataGridView1.Rows[this.dataGridView1.CurrentRow.Index + 1]; for (int i = 0; i < row.Cells.Count; i++) { MessageBox.Show(row.Cells[i].Value.ToString()); } }第一条, if (this.dataGridView1.Rows.Count > 0) { DataGridViewRow row = this.dataGridView1.Rows[0]; for (int i = 0; i < row.Cells.Count; i++) { MessageBox.Show(row.Cells[i].Value.ToString()); } }最后一条 if (this.dataGridView1.Rows.Count > 0) { DataGridViewRow row = this.dataGridView1.Rows[this.dataGridView1.Rows.Count-1]; for (int i = 0; i < row.Cells.Count; i++) { MessageBox.Show(row.Cells[i].Value.ToString()); } } 行数 dataGridView1.Rows.Count-1下一行 先判断下是否是最后一行。 上一行dataGridView1.CurrentRow.Index - 1;第一行 0,最后一行dataGridView1.Rows.Count-1-1dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect; dataGridView1.CurrentCell = dataGridView1.Rows[dataGridView1.CurrentRow.Index + 1].Cells[0]; 1楼的未判断currentRow是否为第一行或是最后一行记录.其它的没有问题up this.dataGridView1.Rows.Count.ToString(); 上一条, if (this.dataGridView1.Rows.Count > 1) { DataGridViewRow row = this.dataGridView1.Rows[this.dataGridView1.CurrentRow.Index - 1]; for (int i = 0; i < row.Cells.Count; i++) { MessageBox.Show(row.Cells[i].Value.ToString()); } } 下一条, if (this.dataGridView1.Rows.Count > 1) { DataGridViewRow row = this.dataGridView1.Rows[this.dataGridView1.CurrentRow.Index + 1]; for (int i = 0; i < row.Cells.Count; i++) { MessageBox.Show(row.Cells[i].Value.ToString()); } } 第一条, if (this.dataGridView1.Rows.Count > 0) { DataGridViewRow row = this.dataGridView1.Rows[0]; for (int i = 0; i < row.Cells.Count; i++) { MessageBox.Show(row.Cells[i].Value.ToString()); } } 最后一条 if (this.dataGridView1.Rows.Count > 0) { DataGridViewRow row = this.dataGridView1.Rows[this.dataGridView1.Rows.Count-1]; for (int i = 0; i < row.Cells.Count; i++) { MessageBox.Show(row.Cells[i].Value.ToString()); } }楼上正确 //上一条 if (dgMX.CurrentRow.Index > 0) { int rowIndex = dgMX.CurrentRow.Index; DataTable dtPre = ((DataTable)dgMX.DataSource); DataRow drAdd = dtPre.NewRow(); DataRow drDel = dtPre.NewRow(); for (int i = 0; i < dtPre.Columns.Count; i++) { drAdd[i] = dtPre.Rows[rowIndex][i].ToString(); drDel[i] = dtPre.Rows[rowIndex - 1][i].ToString(); } dtPre.Rows.InsertAt(drAdd, rowIndex - 1); dtPre.Rows.RemoveAt(rowIndex + 1); dgMX.CurrentCell = dgMX.Rows[rowIndex - 1].Cells[1]; } } //下一条 if (dgMX.Rows.Count > 0) { if (dgMX.CurrentRow.Index < dgMX.Rows.Count - 1) { int rowIndex = dgMX.CurrentRow.Index; DataTable dtPre = ((DataTable)dgMX.DataSource); DataRow drAdd = dtPre.NewRow(); DataRow drDel = dtPre.NewRow(); for (int i = 0; i < dtPre.Columns.Count; i++) { drAdd[i] = dtPre.Rows[rowIndex][i].ToString(); drDel[i] = dtPre.Rows[rowIndex + 1][i].ToString(); } dtPre.Rows.InsertAt(drDel, rowIndex); dtPre.Rows.RemoveAt(rowIndex + 2); dgMX.CurrentCell = dgMX.Rows[rowIndex + 1].Cells[1]; } } 你是说实现选中的光标移动吗。 //上一条 private void button1_Click(object sender, EventArgs e) { int index = this.dataGridView1.SelectedRows[0].Index; if (index != 0) this.dataGridView1.CurrentCell = this.dataGridView1.Rows[index - 1].Cells[0]; else MessageBox.Show("已经是最上一条了"); } //下一条 private void button2_Click(object sender, EventArgs e) { int index = this.dataGridView1.SelectedRows[0].Index; if (index != this.dataGridView1.RowCount-1) this.dataGridView1.CurrentCell = this.dataGridView1.Rows[index + 1].Cells[0]; else MessageBox.Show("已经是最后一条了"); } //最后一条 private void button3_Click(object sender, EventArgs e) { this.dataGridView1.CurrentCell = this.dataGridView1[0, 0]; } //第一条 private void button4_Click(object sender, EventArgs e) { this.dataGridView1.CurrentCell = this.dataGridView1[0, this.dataGridView1.RowCount - 1]; } 使用BindingSource组件和BindingNavigator控件。 用上边的方法取得行号。dataGridView的selectrowindex制定就行了。或者用currentrow指定行对象、 一个关于MSMQ消息应用的疑问 CRC校验问题 新手问题,关于事件 Convert.ToString(dr["oi_VNO"])与(string)dr["oi_VNO"])的区别? DataSet筛选数据的问题(在线等!!急!!) 怎么判断一个textbox的输入是float型的? (菜鸟问题)关于MDI多窗体 线程池的几个问题 线程为什么一直Running?? c#中如何操作*.txt文件 BI商务智能 c#调用mfc扩展dll 资源问题
this.dataGridView1.Rows.Count.ToString();
上一条, if (this.dataGridView1.Rows.Count > 1)
{
DataGridViewRow row = this.dataGridView1.Rows[this.dataGridView1.CurrentRow.Index - 1]; for (int i = 0; i < row.Cells.Count; i++)
{
MessageBox.Show(row.Cells[i].Value.ToString());
}
}下一条, if (this.dataGridView1.Rows.Count > 1)
{
DataGridViewRow row = this.dataGridView1.Rows[this.dataGridView1.CurrentRow.Index + 1]; for (int i = 0; i < row.Cells.Count; i++)
{
MessageBox.Show(row.Cells[i].Value.ToString());
}
}第一条, if (this.dataGridView1.Rows.Count > 0)
{
DataGridViewRow row = this.dataGridView1.Rows[0];
for (int i = 0; i < row.Cells.Count; i++)
{
MessageBox.Show(row.Cells[i].Value.ToString());
}
}最后一条 if (this.dataGridView1.Rows.Count > 0)
{
DataGridViewRow row = this.dataGridView1.Rows[this.dataGridView1.Rows.Count-1]; for (int i = 0; i < row.Cells.Count; i++)
{
MessageBox.Show(row.Cells[i].Value.ToString());
}
}
下一行 先判断下是否是最后一行。 上一行dataGridView1.CurrentRow.Index - 1;第一行 0,最后一行dataGridView1.Rows.Count-1-1
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
dataGridView1.CurrentCell = dataGridView1.Rows[dataGridView1.CurrentRow.Index + 1].Cells[0];
其它的没有问题
up
this.dataGridView1.Rows.Count.ToString();
上一条, if (this.dataGridView1.Rows.Count > 1)
{
DataGridViewRow row = this.dataGridView1.Rows[this.dataGridView1.CurrentRow.Index - 1]; for (int i = 0; i < row.Cells.Count; i++)
{
MessageBox.Show(row.Cells[i].Value.ToString());
}
} 下一条, if (this.dataGridView1.Rows.Count > 1)
{
DataGridViewRow row = this.dataGridView1.Rows[this.dataGridView1.CurrentRow.Index + 1]; for (int i = 0; i < row.Cells.Count; i++)
{
MessageBox.Show(row.Cells[i].Value.ToString());
}
} 第一条, if (this.dataGridView1.Rows.Count > 0)
{
DataGridViewRow row = this.dataGridView1.Rows[0];
for (int i = 0; i < row.Cells.Count; i++)
{
MessageBox.Show(row.Cells[i].Value.ToString());
}
} 最后一条 if (this.dataGridView1.Rows.Count > 0)
{
DataGridViewRow row = this.dataGridView1.Rows[this.dataGridView1.Rows.Count-1]; for (int i = 0; i < row.Cells.Count; i++)
{
MessageBox.Show(row.Cells[i].Value.ToString());
}
}
楼上正确
//上一条
if (dgMX.CurrentRow.Index > 0)
{
int rowIndex = dgMX.CurrentRow.Index;
DataTable dtPre = ((DataTable)dgMX.DataSource); DataRow drAdd = dtPre.NewRow();
DataRow drDel = dtPre.NewRow(); for (int i = 0; i < dtPre.Columns.Count; i++)
{
drAdd[i] = dtPre.Rows[rowIndex][i].ToString();
drDel[i] = dtPre.Rows[rowIndex - 1][i].ToString();
}
dtPre.Rows.InsertAt(drAdd, rowIndex - 1);
dtPre.Rows.RemoveAt(rowIndex + 1);
dgMX.CurrentCell = dgMX.Rows[rowIndex - 1].Cells[1];
}
}
//下一条 if (dgMX.Rows.Count > 0)
{
if (dgMX.CurrentRow.Index < dgMX.Rows.Count - 1)
{
int rowIndex = dgMX.CurrentRow.Index;
DataTable dtPre = ((DataTable)dgMX.DataSource); DataRow drAdd = dtPre.NewRow();
DataRow drDel = dtPre.NewRow(); for (int i = 0; i < dtPre.Columns.Count; i++)
{
drAdd[i] = dtPre.Rows[rowIndex][i].ToString();
drDel[i] = dtPre.Rows[rowIndex + 1][i].ToString();
}
dtPre.Rows.InsertAt(drDel, rowIndex);
dtPre.Rows.RemoveAt(rowIndex + 2);
dgMX.CurrentCell = dgMX.Rows[rowIndex + 1].Cells[1];
}
}
private void button1_Click(object sender, EventArgs e)
{
int index = this.dataGridView1.SelectedRows[0].Index;
if (index != 0)
this.dataGridView1.CurrentCell = this.dataGridView1.Rows[index - 1].Cells[0];
else
MessageBox.Show("已经是最上一条了");
} //下一条
private void button2_Click(object sender, EventArgs e)
{
int index = this.dataGridView1.SelectedRows[0].Index;
if (index != this.dataGridView1.RowCount-1)
this.dataGridView1.CurrentCell = this.dataGridView1.Rows[index + 1].Cells[0];
else
MessageBox.Show("已经是最后一条了");
} //最后一条
private void button3_Click(object sender, EventArgs e)
{
this.dataGridView1.CurrentCell = this.dataGridView1[0, 0];
} //第一条
private void button4_Click(object sender, EventArgs e)
{
this.dataGridView1.CurrentCell = this.dataGridView1[0, this.dataGridView1.RowCount - 1];
}
dataGridView的selectrowindex制定就行了。或者用currentrow指定行对象、