I have some code which sets the value of cells in a DataRow by column name i.e.
row["ColumnName"] = someValue;
I want to also set the value for this row in the column immediately to the right of the one found above. Clearly if I was getting the cell by index rather than by column name this would be easy. So is there a way of getting the column index from the column name thus allowing me to do:
row[index + 1] = someOtherValue;
i.e. do I need create some kind of dictionary of column index and column names when the table is initially created, or can I get the index from the column name later on without doing this?
Select("ColumnName = ColumnValue"). FirstOrDefault(); int index = dt. Rows.
function column(). Get the column index of the selected column. Specify if you want to get the column data index (default) or the visible index ( visible ). The column index for the selected column.
You can get the column index from the column name in Pandas using DataFrame. columns. get_loc() method.
IndexOf(String) Gets the index of the column with the specific name (the name is not case sensitive).
You can use DataColumn.Ordinal
to get the index of the column in the DataTable
. So if you need the next column as mentioned use Column.Ordinal + 1
:
row[row.Table.Columns["ColumnName"].Ordinal + 1] = someOtherValue;
This code returns the next column, so the one after ColumnName
, as requested in the question.
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