更新的GridView列数据绑定后? [英] Update GridView Column after databinding?

查看:215
本文介绍了更新的GridView列数据绑定后?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以更新后的数据绑定的GridView列的内容?在SQL列是全名,我需要显示它作为姓氏,名字,而不是姓氏名字,因为它是现在。我知道我可以在SQL做到这一点,但它好像它会是一种痛苦的(<一href=\"http://stackoverflow.com/questions/159567/how-can-i-parse-the-first-middle-and-last-name-from-a-full-name-field-in-sql\">How我可以从一个完整的名称字段在SQL解析名字,中间名和姓?)。我只是想知道是否有这样做更简单的方法。

感谢

好吧,我想它了...对不起。在事件别人需要这个,我添加了一个 OnRowDataBound =MailingList_RowDataBound事件,该事件处理程序中,我打算使用下面的code(改性来解析名称):

 公共无效MailingList_RowDataBound(对象发件人,GridViewRowEventArgs E)
    {
        //要求忽略页眉/页脚。
        如果(e.Row.RowType == DataControlRowType.DataRow)
        {
            //获取这个人的名字
            字符串名称= e.Row.Cells [0]。文本;
            //添加你的JavaScript onClick事件链接。
            e.Row.Attributes.Add(的onClick,LookupEmail('+名称+'));
            e.Row.Cells [0] =。文本测试;
        }
    }

更改测试,以正确的值。


解决方案

 &LT; ASP:的TemplateField的HeaderText =名称&GT;
  &LT;&ItemTemplate中GT;
    &LT;跨度&GT;
       &LT;%#的eval(全名),斯普利特('')[0]%GT。
    &LT; / SPAN&GT;
   &LT; / ItemTemplate中&GT;
&LT; / ASP:的TemplateField&GT;
&LT; ASP:的TemplateField的HeaderText =姓&GT;
  &LT;&ItemTemplate中GT;
    &LT;跨度&GT;
       &LT;%#的eval(全名),斯普利特('')[1]%&GT;
    &LT; / SPAN&GT;
   &LT; / ItemTemplate中&GT;
&LT; / ASP:的TemplateField&GT;

这应该解决您的问题。假设列名为全名在你的数据库和第一个姓名与空间分隔,您可以拆分的每个值不同的列 GridView控件。

Can I update a gridview column's content after databind? 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. I know that i can do this in sql, but it seems as though it is going to be kind of a pain (How can I parse the first, middle and last name from a full name field in 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):

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.

这篇关于更新的GridView列数据绑定后?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆