winfrom 获取dataGridView1选中行数据

2024-01-03 17:40:56

方法一(dataGridView1.CurrentRow.Index):

        private void btnEdit_Click(object sender, EventArgs e)
        {
            try
            {
                if (dataGridView1.SelectedRows.Count > 0)
                {
                    int RowIndex = dataGridView1.CurrentRow.Index;
                    infrared_heat model = new infrared_heat();
                    model.UUID = Tools.ToString(this.dataGridView1.Rows[RowIndex].Cells[1].Value);
                    model.test_date = (DateTime)Tools.ToDateTime(this.dataGridView1.Rows[RowIndex].Cells[2].Value);
                    model.station_name = Tools.ToString(this.dataGridView1.Rows[RowIndex].Cells[3].Value);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

方法二(e.RowIndex 要找对事件):

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (dataGridView1.SelectedRows.Count > 0)
                {
                    infrared_heat model = new infrared_heat();
                    model.UUID = Tools.ToString(this.dataGridView1.Rows[e.RowIndex].Cells[1].Value);
                    model.test_date = (DateTime)Tools.ToDateTime(this.dataGridView1.Rows[e.RowIndex].Cells[2].Value);
                    model.station_name = Tools.ToString(this.dataGridView1.Rows[e.RowIndex].Cells[3].Value);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

文章来源:https://blog.csdn.net/djk8888/article/details/135358989
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。