简体   繁体   English

如何从Winforms DataGrid的选定行中获取文本?

[英]How to get the text from a selected row on Winforms DataGrid?

This seems like it would be easy but I can't find a way to retrieve the text from the selected row on a DataGrid. 这似乎很容易,但是我找不到从DataGrid上的选定行中检索文本的方法。 The grid is single row selected only - no multiple row selection is allowed. 网格仅是单行选择-不允许多行选择。

Figured it out. 弄清楚了。 One way is 一种方法是

string val = (string)dataGrid1[1, 1]; 字符串val =(string)dataGrid1 [1,1]; // cell 1, row 1 //储存格1,第1行

This is how you get the text of the entire row (as opposed to the existing answer that shows how to get a single value from a DataGrid): 这是您获取整行文本的方式(与现有的答案相反,该答案显示了如何从DataGrid中获取单个值):

string str = "";
int row = datagrid.CurrentRowIndex;
int col = 0;
while (true)
{
    try
    {
        str += datagrid[row,col].ToString() + "|";
        col++;
    }
    catch
    {
        break;
    }
}

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

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