【问题标题】:DataGridView selected row to display in text boxesDataGridView 选定的行显示在文本框中
【发布时间】:2015-04-28 02:41:02
【问题描述】:

我有一个DataGridView(tblLoggedJobs),它显示用户记录的作业列表。我需要管理员能够更新这些作业以显示作业的任何更新或说明作业是否已关闭。 我希望程序将所选行中的数据显示到右侧的文本框中,但是我不确定如何获取此数据并根据所选行显示它。

【问题讨论】:

    标签: c# datagridview


    【解决方案1】:

    您可以使用SelectedRows 属性。

    例子:

    if(dataGridView1.SelectedRows.Count > 0) // make sure user select at least 1 row 
    {
       string jobId = dataGridView1.SelectedRows[0].Cells[0].Value + string.Empty;
       string userId = dataGridView1.SelectedRows[0].Cells[2].Value + string.Empty;
    
       txtJobId.Text = jobId;
       txtUserId.Text = userId;
    }
    

    【讨论】:

    • 如果用户选择了多行怎么办?
    • @Ben "如果用户选择了多行怎么办?" Datagridview 的属性 -> MultiSelect = False
    • 效果很好!在每个变量声明语句末尾添加“string.Empty”的目的是什么?
    • dataGridView1.SelectedRows[0].Cells[0].Value 返回Object 所以你需要将它转换为String
    • 这是否意味着我可以将String 换成intDateTime
    【解决方案2】:

    在 cellclick 事件中添加代码

    if (e.RowIndex >= 0)
    {
        //gets a collection that contains all the rows
        DataGridViewRow row = this.dataGridView1.Rows[e.RowIndex];
        //populate the textbox from specific value of the coordinates of column and row.
        txtid.Text = row.Cells[0].Value.ToString();
        txtfname.Text = row.Cells[1].Value.ToString();
        txtlname.Text = row.Cells[2].Value.ToString();
        txtcourse.Text = row.Cells[3].Value.ToString();
        txtgender.Text = row.Cells[4].Value.ToString();
        txtaddress.Text = row.Cells[5].Value.ToString();
    }
    

    欲了解更多信息,请使用此链接How to Display Selected Row from Datagridview into Textbox using C#

    【讨论】:

      【解决方案3】:
      private void orderpurchasegridview_CellClick(object sender, DataGridViewCellEventArgs e)
          {
      
              int ind = e.RowIndex;
              DataGridViewRow selectedRows = orderpurchasegridview.Rows[ind];
              txtorderid.Text = selectedRows.Cells[0].Value.ToString();
              cmbosuppliername.Text = selectedRows.Cells[1].Value.ToString();
              cmboproductname.Text = selectedRows.Cells[2].Value.ToString();
              txtdate.Text = selectedRows.Cells[3].Value.ToString();
              txtorderquantity.Text = selectedRows.Cells[4].Value.ToString();
      }
      

      【讨论】:

        【解决方案4】:
           private void MyDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                DataGrid dg = (DataGrid)sender;
                DataRowView rs = dg.SelectedItem as DataRowView;
                if(rs != null)
                {
                    textBoxJobId.Text = rs["JobId"].ToString();
                }
            } 
        

        窥探频道https://www.youtube.com/user/saf3al2a/videos

        【讨论】:

          猜你喜欢
          • 2011-11-13
          • 1970-01-01
          • 1970-01-01
          • 2015-04-18
          • 1970-01-01
          • 2012-10-17
          • 1970-01-01
          • 2015-07-12
          • 2015-06-28
          相关资源
          最近更新 更多