简体   繁体   中英

DataFormatString in CodeBehind

I want to fix the way in which dates shows in my Gridview, I have tried this:

private void Temp_RowDataBound(object sender, GridViewRowEventArgs e)     
{
        if(e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[6].Attributes.Add("DataFormatString", "{0:d}");
        }
}

Since the gridvirws are generating while runnig, I can't do it as i would normally do:

<asp:BoundField DataFormatString="{0:d}" DataField="Date" HeaderText="Date" ReadOnly="True" SortExpression="Date" />

so I can II do it? Thanks!

If you don't know how many grids your page will have, it might be a good idea to use a Repeater . If you do so, you will be able to use the DataFormatString property of the BoundField like you said.

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