天天看点

datagridview显示问题

1.获取索引行与显示问题

int index = dataGridView1.CurrentRow.Index;    //获取点击索引行

dataGridView1.Rows[index].Selected = true;     //加亮显示

string curNo = this.dataGridView1.Rows[index].Cells[0].Value.ToString();   //取索引行的第一个单元格内容

int r = Convert.ToInt32(this.dataGridView1["Record_no", index].Value.ToString());

string no = this.dataGridView1["InBound_no", index].Value.ToString();

2.

 int i = dataGridView1.CurrentRow.Index;       //获取当前记录的索引号 

 this.dataGridView1.Rows[i].Selected = true;  //加亮显示

 string curN = this.dataGridView1["ZGOutNo", i].Value.ToString();

3.

 private void showData()

        {

            try

            {

                conn = new OracleConnection(ConnectionString);

                da = new OracleDataAdapter("SELECT Record_no,Tool_no,entrustDate,finishDate,Factory,beforeSize1,afterSize1,beforeSize2,afterSize2,beforeSize3,"

                    + "afterSize3,beforeSize4,afterSize4,beforeSize5,afterSize5,importer,Insert_Time FROM T_ZG_maintenance", conn);

                ds = new DataSet();

                da.Fill(ds);

                dataGridView2.DataSource = ds;

                dataGridView2.DataMember = ds.Tables[0].ToString();

            }

            catch(Exception ex)

            {

                MessageBox.Show(ex.ToString());

            }

            finally

            {

                ds.Dispose();

            }

        }

4.

 private void ShowData()

        {

            try

            {

                 conn = new OracleConnection(ConnectionString);

                da = new OracleDataAdapter(string.Format("SELECT Record_no,Tool_no,entrustDate,finishDate,Factory,beforeSize1,afterSize1,beforeSize2,afterSize2,beforeSize3,"

                    + "afterSize3,beforeSize4,afterSize4,beforeSize5,afterSize5,importer,Insert_Time FROM T_ZG_maintenance Where Tool_no='{0}'", cmbTool_no.Text), conn);

                ds = new DataSet();

                da.Fill(ds);

                da.Dispose();

                this.bindingSource2.DataSource = ds.Tables[0];

                this.bindingNavigator2.BindingSource = bindingSource2;

                this.dataGridView2.DataSource = bindingSource2;

            }

            catch (Exception ex)

            {

                MessageBox.Show(ex.ToString());

            }

            finally

            {

                ds.Dispose();

            }

        }

5.点击datagridview索引超出范围,必须为非负值并小于集合大小。

dgvTicketInfo.SelectedRows[0].Cells["ID"].Value报错

因为dgvTicketInfo.SelectedRows的默认selectionMode是 RowHeaderSelect,如果你点击行中某一个单元格,是选不到整行的!所以dgvTicketInfo.SelectedRows.Count实际上是零。你的那句话:

dgvTicketInfo.SelectedRows[0] 肯定要报索引超出范围了!

可以把selectionMode设成整行选择!当然也可以改进你的定位行和单元格的方式!比如通过当前单元格的行列下标,定位你的“ID”也可以!