c# - is it possible to read the column name of a cell in gridview? -


I want to find the column name of the cell in the event below a datagrid view.

  protected void grvDetailedStatus_ItemDataBound (object sender, DataGridItemEventArgs e) {(int i = 0; i & lt; = e.Item.Cells.Count - 1; i {System for ++) .DateTime cellDate = default (System.DateTime); If (System.DateTime.TryParse (e.Item.Cells [i] .text, cellDate out)) {e.Item.Cells [i] .text = String.Format ( "{0: d}", cellDate); }}}  

Am I getting a way to get the name of the cell column?

Edit: Sorry for not giving a clear explanation. Let me explain it more clearly.

I only want to do the formatting given below for specific column values:

  protected void grvDetailedStatus_ItemDataBound (object sender, DataGridItemEventArgs e) {(Int i = 0; i & lt; = e.Item.Cells.Count - 1; i ++) {System .DateTime CellDet = Default (System.DateTime); If (System.DateTime.TryParse (e.Item.Cells [i]. Text, out of salad)) {ITOM. Seals [i]. Text = string Format ("{0: d}", celadet); For example, I want to know will only change the format of date, say,}}}  

So now I column name "Column 1" and "Column 5" and that Along with, I want to leave the column and format the column alone.

  protected void grvDetailedStatus_ItemDataBound (object sender, DataGridItemEventArgs e) {if (columnName == "column 1") {for (int i = 0; i & lt; = e.Item.Cells. Count - 1; i ++) {System.DateTime cellDate = Default (System.DateTime); If (System.DateTime.TryParse (e.Item.Cells [i]. Text, out of salad)) {ITOM. Seals [i]. Text = string Format ("{0: d}", celadet); }}}}  

.

I have been using a Detagrid you will receive Gridwu a similar control, but slightly different flavor . I would also like to assume that you are using AutoGenated columns, in this situation, the DataGrid.Column collection will not help you.

Instead of checking the column names every time, it is better to store the index of the columns you are interested in at a time like this:

Private list & lt; Int & gt; _myColumns; Protected void grvDetailedStatus_ItemDataBound (Object Sender, DataGridItemEventArgs e) {if (e.Item.ItemType == ListItemType.Header) {_myColumns = New list of & lt; Integer & gt; (); (Int i = 0; I & lt; _columnNames.Length; i ++) {switch (e.Item.Cells [i] .text) {Case "column 1": Case "column5": // Interesting column , Store Index _myColumns. Couples (i); break; }}} Any other (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) {foreach (int in i_myColumns) if {// your original code: System.DateTime cellDate = Default (System.DateTime); If (System.DateTime.TryParse (e.Item.Cells [i]. Text, out of salad)) {ITOM. Seals [i]. Text = string Format ("{0: d}", celadet); }}}

You really, it will be quite easy to optimize this code (or view the previous version of this post). If you wanted to store all the column names, then


Comments

Popular posts from this blog

c# - ListView onScroll event -

PHP - get image from byte array -

Linux Terminal Problem with Non-Canonical Terminal I/O app -