Asp.Net网格-在网格行的大写字母中创建第一个字符

时间:2018-12-07 12:09:28

标签: asp.net

下面的代码工作正常,并将数据表绑定到asp.net网格

        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        Grid1.DataSource = ds;
        Grid1.DataBind();

                 <asp:GridView ID="Grid1" runat="server" GridLines="Both" CellPadding="4" OnRowDataBound="Grid1_OnRowDataBound">
                 </asp:GridView>

网格中的第一行,显示类似这样的文本-“棕色狐狸跳得很快”

如何在网格中显示它-“褐狐快速跳跃”

第一个字符必须大写。

1 个答案:

答案 0 :(得分:2)

您可以处理RowDataBound事件。

假设您使用BoundFields并且文本在第一列:

void CustomersGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
{
    if(e.Row.RowType == DataControlRowType.DataRow)
    {
        string oldText = e.Row.Cells[0].Text;
        if(!string.IsNullOrWhiteSpace(oldText))
            e.Row.Cells[0].Text = char.ToUpper(oldText[0]) + (oldText.Length > 1 ? oldText.Substring(1) : "");
    }
}