开发者

work with grid view

<asp:GridView ID="gridInboxMessage" runat="server" 
            AutoGenerateColumns="False" 
            DataSourceID="LinqDataSource1">
    <Columns>
        <asp:BoundField DataField="Title" HeaderText="title" ReadOnly="True" SortExpression="Title" />
        <asp:BoundField DataField="Body" HeaderText="body" ReadOnly="True" SortExpression="Body" />
        <asp:BoundField DataField="Sender" HeaderText="sender" ReadOnly="True" SortExpression="Sender" />
        <asp:BoundField DataField="Date1" HeaderText="date" ReadOnly="True" SortExpression="Date1" />
    </Columns>
</asp:GridView>
<asp:LinqDataSource ID="LinqDataSource1" runat="server" 
            ContextTypeName="DataClassesDataContext"
            Select="new (Title, Body, Sender, Date1)" 
            TableName="PrivateMessages" 
            Where="Receptor == @Receptor">
    <WhereParameters>
        <asp:QueryStringParameter Name="Receptor" QueryStringField="idCompany" Type="String" />
    </WhereParameters>
</asp:LinqDataSource>

I have an asp:GridView populated from an LinqDataSource. My questions are

  1. Body included is 1000 characters I can display only 50 characters in the field of body(over flow hide).
  2. field date content 1/1/2011 i want show jul 1 2011 in field date
  3. field sender equal id (example 23) i want show name(23=alen)

How will I achieve all the开发者_如何转开发se?

Edit

answer @naveen is correct.

i want when user click on row show body full????


Try this.

Markup

<asp:GridView ID="gridInboxMessage" runat="server" 
            AutoGenerateColumns="False" 
            DataSourceID="LinqDataSource1">
    <Columns>
        <asp:BoundField DataField="Title" HeaderText="title" ReadOnly="True" SortExpression="Title" />

        <asp:TemplateField HeaderText="Body" SortExpression="Body">
            <ItemTemplate>
                <asp:Label ID="MyBody" runat="server"
                        Text='<%# TruncateText(Eval("Body"))%>'>
                </asp:Label>
            </ItemTemplate>
        </asp:TemplateField>

        <asp:TemplateField HeaderText="Sender">
            <ItemTemplate>
                <asp:Label ID="MySender" runat="server"
                        Text='<%# GetSenderNameFromID(Eval("Sender"))%>'>
                </asp:Label>
            </ItemTemplate>
        </asp:TemplateField>

        <asp:TemplateField HeaderText="Date" SortExpression="Date1">
            <ItemTemplate>
                <asp:Label ID="MyDate" runat="server"
                        Text='<%# DataBinder.Eval(Container.DataItem, "Date1", "{0:MMMM d yyy}")%>'>
                </asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

Code Behind

protected string TruncateText(object objBody)
{
    string truncated = "";
    if (objBody != null)
    {
        truncated = objBody.ToString().Length > 50 ? 
            objBody.ToString().Substring(0, 47) + "..." : objBody.ToString();
    }
    return truncated;
}

protected string GetSenderNameFromID(object objSenderID)
{
    string senderName = "";
    if (objSenderID != null)
    {
        senderName = CallDatabaseToGetNameFromID();
    }
    return senderName;
}

private string CallDatabaseToGetNameFromID()
{
    //implement your database call to retrieve sender name from id
    throw new NotImplementedException();
}

Hope this helps.


First of all, if you want to show the Name of sender and not ID, you have to modify your query to join in the table that contains the name

For date issue, you can use Format(Date, "dd/mm/yyyy")

Can you ensure that your query actually returns all the characters before binding to the grid?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜