views:

125

answers:

2

Hi 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 manupulating.

Please help .

Hi, Sorry for not giving a clear explanation.Let me explain it more clearly.

I want to do the below formating 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 alsone 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);
            }
        }
    }
}

.

A: 

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
    }
}
Keltex
Hi sorry i have updated the question more clearly.Please help.
Jebli
Your code was useful to me
Jebli
A: 

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).

Thorarin
Care to comment on the downvote? This does exactly what he asked for?
Thorarin
I didn't downvote, but I'd wager that you got it because he asked about GridViews. You edited his post to include a tag for DataGrid, and answered in context of that.
JoshJordan
But he **is** using a DataGrid. GridView has no ItemDataBound event, and *doesn't* use `DataGridItemEventArgs`. Don't blame me for being observant :)
Thorarin