0

I am using this line of code

a = row.Cells[5].Text;
b = row.Cells[6].Text;
c = row.Cells[9].Text;
d = row.Cells[12].Text;

it is working fine.

but my problem is when I add new column to the grid or delete one or change columns order.

in that case the each line of code has to change as the order of the columns have changes.

is there any way I can use Column name instead of number?

Cleptus
  • 3,446
  • 4
  • 28
  • 34
asmgx
  • 7,328
  • 15
  • 82
  • 143
  • I have used gridview long time back. I think, you will get your solution in link https://stackoverflow.com/questions/25368870/get-the-cell-value-of-a-gridview-row – m.k.sharma Jul 29 '20 at 10:02
  • Is it `System.Windows.Controls.GridView` or `System.Web.UI.WebControls.GridView`?. Consider editing the question and adding more tags because the answer may vary on the specific UI – Cleptus Jul 29 '20 at 10:22
  • @Cleptus it is a Web control – asmgx Jul 29 '20 at 10:25
  • Does this answer your question? [How to get the cell value by column name not by index in GridView in asp.net](https://stackoverflow.com/questions/9715983/how-to-get-the-cell-value-by-column-name-not-by-index-in-gridview-in-asp-net) – Cleptus Jul 29 '20 at 10:47

1 Answers1

1

You should add a custom attribute to each cell in order to identify it. Next do a loop over row.Cells and a row.Cells[i] returns a DataControlFieldCell object. Here you have Attributes and you will find your custom attribute. In this way you are able to understand what cell it is.

D A
  • 1,724
  • 1
  • 8
  • 19