简体   繁体   中英

Get column index and current value from datagridview c#

How to get current column index and current value from this foreach loop? Please, I need to finish my school task.

foreach (DataGridViewRow row in tradeGridView.Rows)
{

}

this code iterate all the datagrid rows, if you want to retrive the selected columns tradeGridView.SelectedColumns(); You can try this

foreach (DataGridViewRow row in tradeGridView.Rows)
{
        for (int i = 0; i < tradeGridView.SelectedColumns.Count; i++)
        {
            //INDEX
            var index = tradeGridView.SelectedColumns[i].Index;
            //VALUE
            var value = row[index]
        }
}

if you want only the selected row change tradeGridView.Rows with tradeGridView.SelectedRows

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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