views:

67

answers:

1

I have a LinqDataSource and a GridView displaying a table. Columns of type xml don't show up. I'd like them to show up something like they do in Sql Server query outputs, with a link to display the clickable xml, though there may be another approach i'm not considering (maybe a styled display of the xml data, etc.). Two things i'd like to know how to do

  • First, get the xml converted to a string, and display it in the table. Maybe the first 30 chars.
  • Finally, style the xml into something useful, like a clickable link to display the full xml, or a sub-table, or a styled string.

So the following works, and displays a nice table, with edit and delete links. But Xml fields are missing. How would you go about adding support for the Xml fields?

<form id="form1" runat="server">
<div>
    <asp:LinqDataSource ID="OrdersDataSource"
        OnContextCreating="LinqDataSource_ContextCreating"
        runat="server" ContextTypeName="MyDbDataContext"
        EnableUpdate="True" TableName="orders"
        EnableDelete="true"
        OrderBy="Status, UserId">
    </asp:LinqDataSource>
    <asp:GridView ID="OrdersGridView" DataSourceID="OrdersDataSource"
        CssClass="gridview" PageSize="30" AutoGenerateDeleteButton="true"
        AutoGenerateEditButton="true" AllowPaging="True" AllowSorting="True"
        AlternatingRowStyle-BackColor="Beige" DataKeyNames="OrderId"
        runat="server" Width="705px">
        <Columns>
        </Columns>
    </asp:GridView>
</div>
</form>

Page_Load is empty at the moment.

A: 

The best approach would be to use GridView's RowDataBound event. This would look something like this:

protected void OrdersGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        var dataItem = e.Row.DataItem;
        ...
    }
}

I'm not sure what the type of dataItem is, but probably you can cast it to your order type (easiest to see in the debugger, just set a breakpoint in the RowDataBound event handler). You should be able to get your xml data from this object. When you have the xml data, you can convert it to a string and get the first 30 characters, for example.

The last thing to do is set this text in the correct cell. See the example on MSDN for this.

Ronald Wildenberg