简体   繁体   English

C#-在DataGridView中选择列

[英]C# - Selecting a Column in a DataGridView

I am creating a football roster using C# and Visual Studio. 我正在使用C#和Visual Studio创建足球花名册。 I've created a DataGridView calling dgvPlayer. 我创建了一个调用dgvPlayer的DataGridView。

DGV DGV

My goal is to select a box on dgvPlayer and have the information be copied into a Name textbox, Team textbox, etc. I'm really just confused as to the how to identify that a certain box is being selected. 我的目标是在dgvPlayer上选择一个框,然后将信息复制到“名称”文本框,“团队”文本框等中。我真的只是对如何识别某个框被选中感到困惑。 Please help! 请帮忙!

dgvPlayer.Columns["Name"].DefaultCellStyle.Format

Based on what you are trying to do, you will have to add a handler for the CellClick event. 根据您要执行的操作,您将必须为CellClick事件添加一个处理程序。 The event will fire everytime the user make a click on some cell, then you will have to put your own business logic there. 每当用户单击某个单元格时,都会触发该事件,然后您将不得不在其中放置自己的业务逻辑。 You can do something like this: 您可以执行以下操作:

dgvPlayer.CellClick += dgvPlayer_CellClick;

private void dgvPlayer_CellClick(object sender, DataGridViewCellEventArgs e)
{
     yourTextBox.Text = dgvPlayer.Rows[e.RowIndex].Cells[e.ColumnIndex].ToString();
}

Something like this that adds some validation against selecting the header row of the grid view 像这样的东西增加了一些针对选择网格视图标题行的验证

private void dgvPlayer_CellClick(object sender, DataGridViewCellEventArgs e)
{
        if (e.RowIndex != -1)
        {
            txtTextBox.Text = dgvPlayer.Rows[e.RowIndex].Cells[e.ColumnIndex].ToString();          
        }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM