I have a datagrid having few columns-
The header of the grid is hyperlink and I am setting its value at runtime as follows-
string strQ1 = "<a href='somePage.aspx?ID=1'>gfgytyty<a>";
dtGrid.Columns[0].Header = strq1;
string strQ2 = "<a href='somePage.aspx?ID=2'>yhtryrtuyu<a>";
dtGrid.Columns[1].Header = strq2;
and so on...
It is working properly. Now suppose I want to get index of a perticular column of datatgrid by its name but I am not able to get it. I tried
int colIndex = dtGrid.Columns.IndexOf(dtGrid.Columns[strQ2]);
this should return 1 as columnIndex but it is returning -1,
Also, dtGrid.Columns[strQ2] giving me the null value.
what I am doing wrong here?
Use DataFrame. loc[] and DataFrame. iloc[] to select a single column or multiple columns from pandas DataFrame by column names/label or index position respectively. where loc[] is used with column labels/names and iloc[] is used with column index/position.
You could use LINQ FirstOrDefault to get the object first and only then use .IndexOf(object) :
var targetColumn = dtGrid.Columns.FirstOrDefault(c => c.Header == strQ2);
var index = dtGrid.Columns.IndexOf(targetColumn);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With