简体   繁体   中英

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 below event of a datagridview.

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

Is there any way to find the column name of the cell I am manipulating?

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

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

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

        }
    }
}

Say, for example, I have to change the format of the date only for the "column1" and "column5". So now I want to know the column name and with that I want to format that column alone and leave the rest.

protected void grvDetailedStatus_ItemDataBound(object sender, DataGridItemEventArgs e)
{
    if ( columnName == "Column1")
    {
        for (int i = 0; i <= e.Item.Cells.Count - 1; i++)
        {
            System.DateTime cellDate = default(System.DateTime);
            if (System.DateTime.TryParse(e.Item.Cells[i].Text, out cellDate))
            {
                e.Item.Cells[i].Text = string.Format("{0:d}", cellDate);
            }
        }
    }
}

.

I'll assume you're using a DataGrid. GridView is a similar control, but slightly different flavor. I'll also assume you're using autogenerated columns, in which case the DataGrid.Columns collection won't help you.

Instead of checking the column names each time, it's better to store the indexes of the columns you're interested in once. Like this:

private List<int> _myColumns;

protected void grvDetailedStatus_ItemDataBound(object sender,
    DataGridItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Header)
    {
        _myColumns = new List<int>();

        for (int i = 0; i < _columnNames.Length; i++)
        {
            switch (e.Item.Cells[i].Text)
            {
                case "column1":
                case "column5":
                    // Interesting column, store index
                    _myColumns.Add(i);
                    break;
            }
        }
    }
    else if (e.Item.ItemType == ListItemType.Item ||
        e.Item.ItemType == ListItemType.AlternatingItem)
    {
        foreach (int i in _myColumns)
        {
            // Your original code:
            System.DateTime cellDate = default(System.DateTime);
            if (System.DateTime.TryParse(e.Item.Cells[i].Text, out cellDate))
            {
                e.Item.Cells[i].Text = string.Format("{0:d}", cellDate);
            }
        }
    }
}

If you really wanted to store all the column names, it would be fairly easy to adapt this code (or look at an earlier version of this post).

If you're running through the individual cells during the ItemDataBound event and need to get the column name of a cell then you can use the following:

protected void myDataGrid_ItemDataBound(object sender, DataGridItemEventArgs e)
{
    int i = 0;
    string colName;

    foreach (TableCell cell in e.Item.Cells)
    {
        //string cellTxt = e.Item.Cells[i].Text;
        DataRowView dv = (DataRowView)e.Item.DataItem;
        colName = dv.DataView.Table.Columns[i].ColumnName;
        i++;
    }
}

First you need to check whether the e.Item is an ListItemType.Item or ListItemType.AlternatingItem or ListItemType.Header . If it's a Header, then you can get the title as follows:

protected void grvDetailedStatus_ItemDataBound(object sender, DataGridItemEventArgs e)
{ 
    if(e.Item.ItemType == ListItemType.Header))
    {
        // retrieve the header text... e.g.
        string FirstCellHeader = e.Item.Cells[0].Text;
    }
    else if((e.Item.ItemType == ListItemType.Item) || 
         (e.Item.ItemType == ListItemType.AlternatingItem))
    {
        // your code for items
    }
}

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