简体   繁体   English

数据绑定后更新GridView列?

[英]Update GridView Column after databinding?

Can I update a gridview column's content after databind? 我可以在数据绑定后更新gridview列的内容吗? The SQL column is a full name, and I need to display it as last name, first name, not first name last name, as it is now. SQL列是一个全名,我需要将它显示为姓氏,名字,而不是名字姓氏,就像现在一样。 I know that i can do this in sql, but it seems as though it is going to be kind of a pain ( SQL: parse the first, middle and last name from a fullname field ). 我知道我可以在sql中执行此操作,但似乎有点痛苦( SQL:解析全名字段中的第一个,中间名和姓氏 )。 I was just wondering if there is an easier way to do this. 我只是想知道是否有更简单的方法来做到这一点。

Thanks 谢谢

Ok, I figured it out... sorry. 好吧,我想通了......抱歉。 In the event someone else needs this, I added a OnRowDataBound="MailingList_RowDataBound" event, and within that event handler, I have am going to use the following code (modified to parse the name): 如果其他人需要这个,我添加了一个OnRowDataBound="MailingList_RowDataBound"事件,并且在该事件处理程序中,我将使用以下代码(修改为解析名称):

public void MailingList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        // Required to ignore the header/footer.
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //Get the Person's Name
            string name = e.Row.Cells[0].Text;
            //Add your JavaScript onClick event link.
            e.Row.Attributes.Add("onClick", "LookupEmail('" + name + "')");
            e.Row.Cells[0].Text = "Test";
        }
    }

Changing the "Test" to the correct values. 将“测试”更改为正确的值。

<asp:TemplateField HeaderText="Name">
  <ItemTemplate>
    <span>
       <%# Eval("FullName").Split(' ')[0]%>
    </span>
   </ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Surname">
  <ItemTemplate>
    <span>
       <%# Eval("FullName").Split(' ')[1]%>
    </span>
   </ItemTemplate>
</asp:TemplateField>

This should solve your problem. 这应该可以解决您的问题。 Assuming the column name is FullName in your database and the first name and surname are seperated with space, you can split those and bind each value to different columns in GridView . 假设数据库中的列名称为FullName ,并且第一个名称和姓氏与空格分隔,您可以拆分它们并将每个值绑定到GridView不同列。

use itemtemplate 使用itemtemplate

            <asp:GridView ID="tset" runat="server">
               <Columns>
                  <asp:TemplateField HeaderText="Name">
                    <ItemTemplate>
                       <asp:Label ID="label1" runat="server"  Text='<%#GetFormattedName(Eval("full_name")%>'></asp:Label>
                    </ItemTemplate>
                  </asp:TemplateField>
               </Columns>
            </asp:GridView>

and write GetFormattedName method in code behind to return a string however you want, 并在代码后面写GetFormattedName方法返回一个你想要的字符串,

    protected string GetFormattedName(string fullName)
    {
          // you can split full name here and swap first name, last name
          // depending on how you store it in DB 
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM