How Handle Null Values(In Columns) In Telerik RadG

2019-08-03 02:34发布

问题:

i have a BooundColumn in my telerik RadGrid like below :

                    <telerik:GridBoundColumn DataField="Status" FilterControlAltText="Filter Status column"
                        HeaderText="Status" SortExpression="Status" UniqueName="Status"
                        FilterImageToolTip="Filter">
                        <HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" />
                        <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
                    </telerik:GridBoundColumn>

Status Column in my sql server 2008 database can accept null values and RadGrid shows them with an empty string.
how can i replace these Empty Strings with a Colorable Strings?
mean replacement with something like this -> <span style='color: red;'>Empty</span> i know i can convert that BoundColumn to TemplateColumn like this :

                        <telerik:GridTemplateColumn FilterControlAltText="Filter TemplateColumn_Status column"
                            HeaderText="Status" UniqueName="TemplateColumn_Status" FilterImageToolTip="Filter"
                            ReadOnly="False" Visible="False">
                            <ItemTemplate>
                                <asp:Label ID="lblStatusInsideGrd" runat="server" Font-Size="11px" Text='<%# (bool)Convert.IsDBNull(Eval("Status")) ? "<span class=\"lblInsideGrd\">Empty</span>" : Eval("Status") %>'></asp:Label>
                            </ItemTemplate>
                            <HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" />
                            <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
                        </telerik:GridTemplateColumn>

this replacement is a good idea and works great. but by doing that i lose RadGrid Filering on this column.
would you please showe me another way with keeping RadGrid Friltering?

thanks in advance

回答1:

You could do that on ItemDataBound event, when grid is binded to SqlDataSource as in your case you can get current DataRow :

Update: I leaved just code that works with DataRow, and fix column background setting

public void TestGrid_ItemDataBound(Object sender, Telerik.Web.UI.GridItemEventArgs e)
{
  if (e.Item is GridDataItem)
  {
    if (e.Item.DataItem is DataRowView)
    {
      GridDataItem gdItem = (GridDataItem)e.Item;
      DataRow rw = ((DataRowView)e.Item.DataItem).Row;
      if (rw.IsNull("Status"))
      {
        GridColumn urlColumn = TestGrid.MasterTableView.Columns.FindByUniqueName("Status");
        gdItem.Cells[urlColumn.OrderIndex].BackColor = Color.Red;
        gdItem["Status"].Text = "Empty";
      }
    }
  }
}

EDIT BY QUESTIONER
Really thanks for these codes.
the codes below are ok for me :

protected void grdSMS_ItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridDataItem)
    {
        GridDataItem item = (GridDataItem)e.Item;
        if (item["Status"].Text == "&nbsp;")
        {
            item["Status"].ForeColor = Color.Red;
            item["Status"].Text = "Empty";
        }
    }
}