C# DataGridView 选择的行显示在文本框中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29909352/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 04:22:44  来源:igfitidea点击:

DataGridView selected row to display in text boxes

c#datagridview

提问by Matt List

I have a DataGridView(tblLoggedJobs) that displays a list of jobs logged by a user. I need the admins to be able to update these jobs to display any updates to the job or note if the job is closed. I would like the program to display the data in the selected ROW to the textboxes to the right, however I'm not sure how to get this data and display it based on the row that is selected.

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

Get Selected Row to text boxes

获取选定行到文本框

采纳答案by Iswanto San

You can use SelectedRowsproperty.

您可以使用SelectedRows属性。

Example:

例子:

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

回答by Pradnya Bolli

Select cellclick event of datagridview

选择datagridview的cellclick事件

Add code in cellclick event

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

For More information use this link How to Display Selected Row from Datagridview into Textbox using C#

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

回答by Muhammad Zohaib

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

回答by Sergey Gladchenko

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

spied on the channel https://www.youtube.com/user/saf3al2a/videos

监视频道https://www.youtube.com/user/saf3al2a/videos