我想用按钮实现读取dataGridView的行数,上一条,下一条,第一条,最后一条的功能,怎样实现呢???

解决方案 »

  1.   

    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());
                    }
                }
      

  2.   

    行数 dataGridView1.Rows.Count-1
    下一行 先判断下是否是最后一行。 上一行dataGridView1.CurrentRow.Index - 1;第一行 0,最后一行dataGridView1.Rows.Count-1-1
    dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;   
                dataGridView1.CurrentCell   =   dataGridView1.Rows[dataGridView1.CurrentRow.Index + 1].Cells[0];
      

  3.   

    1楼的未判断currentRow是否为第一行或是最后一行记录.
    其它的没有问题
    up
      

  4.   


       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()); 
                    } 
                }
    楼上正确
      

  5.   


    //上一条
      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];
                    }
                }
      

  6.   

    你是说实现选中的光标移动吗。      //上一条
            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];
            }
      

  7.   

    使用BindingSource组件和BindingNavigator控件。
      

  8.   

    用上边的方法取得行号。
    dataGridView的selectrowindex制定就行了。或者用currentrow指定行对象、