如何在加载DataBound后更改radgrid模板列值

时间:2015-02-27 04:16:40

标签: c# asp.net .net telerik radgrid

我有一个radgrid控件,它有一个itemtemplate使用TextBox的模板列。加载radgrid后,它会显示所有带NULL文本框值的数据。现在我想在加载radgrid之后,应该根据我提供的条件按照记录填充文本框值。

我尝试了OnItemDataBoundOnColumnCreated事件,但我无法获得标签的价值。

代码如下:

<telerik:RadGrid ID="gridmaterial" runat="server" AutoGenerateColumns="false" AllowSorting="false" OnColumnCreated="gridmaterial_OnColumnCreated" OnItemDataBound="gridmaterial_OnItemDataBound"  >
                                    <MasterTableView AutoGenerateColumns="false" ShowHeadersWhenNoRecords="true">
                                        <Columns>
                                            <telerik:GridTemplateColumn UniqueName="RowSelect" AllowFiltering="false">
                                                <HeaderStyle Width="5%" />
                                                <ItemTemplate>
                                                    <asp:CheckBox runat="server" ID="chbxmatrow" ClientIDMode="Static" ViewStateMode="Enabled"/>
                                                </ItemTemplate>
                                            </telerik:GridTemplateColumn>
                                            <telerik:GridTemplateColumn HeaderText="#" UniqueName="SerialNumber" AllowFiltering="false">
                                                <HeaderStyle Width="5%" />
                                                <ItemTemplate>
                                                    <asp:Label runat="server" ID="lblSNo" CssClass="control-label" ClientIDMode="Static" Text='<%# Container.DataSetIndex + 1 %>'></asp:Label>
                                                </ItemTemplate>
                                            </telerik:GridTemplateColumn>
                                                <telerik:GridTemplateColumn HeaderText="Material Id" UniqueName="MasterMatId" Visible="false">
                                                <HeaderStyle Width="40%" />
                                                <ItemTemplate>
                                                <asp:Label runat="server" ID="lblMaterialid" CssClass="control-label" ClientIDMode="Static" Text='<%# Eval("MasterMatID") %>'></asp:Label>
                                                </ItemTemplate>
                                            </telerik:GridTemplateColumn>
                                             <telerik:GridTemplateColumn HeaderText="Group" UniqueName="Group">
                                                <HeaderStyle Width="20%" />
                                                <ItemTemplate>
                                                <asp:Label runat="server" ID="lblMaterialGroup" CssClass="control-label" ClientIDMode="Static" Text='<%# Eval("Group") %>'></asp:Label>
                                                </ItemTemplate>
                                            </telerik:GridTemplateColumn>
                                            <telerik:GridTemplateColumn HeaderText="Material Description" UniqueName="MasterMatDesc">
                                                <HeaderStyle Width="50%" />
                                                <ItemTemplate>
                                                <asp:Label runat="server" ID="lblMaterialDesc" CssClass="control-label" ClientIDMode="Static" Text='<%# Eval("Description") %>'></asp:Label>
                                                </ItemTemplate>
                                            </telerik:GridTemplateColumn>
                                            <telerik:GridTemplateColumn HeaderText="Qty" UniqueName="Qty" AllowFiltering="false">
                                                <HeaderStyle Width="20%" />
                                                <ItemTemplate>
                                                    <asp:TextBox runat="server" CssClass="form-control" ID="txtQty" ViewStateMode="Enabled" Width="100" onKeyPress="return isNumber(event);"></asp:TextBox> 
                                                </ItemTemplate>
                                            </telerik:GridTemplateColumn>

                                        </Columns>

                                    </MasterTableView>
                                    <ClientSettings>
                                        <Scrolling AllowScroll="true" ScrollHeight="400px" />
                                    </ClientSettings>
                                </telerik:RadGrid>

我尝试过的代码:

 protected void gridmaterial_OnItemDataBound(object sender, GridItemEventArgs e)
    {
        foreach (GridDataItem item in gridmaterial.Items)
        {
            CheckBox rowcheck = (CheckBox)item.FindControl("chbxmatrow");
            Label lblMaterialid = (Label)item.FindControl("lblMaterialid");
            Label lblMaterialDesc = (Label)item.FindControl("lblMaterialDesc");
            TextBox txtQty = (TextBox)item.FindControl("txtQty");
            var result = (from r in MapppedItem.AsEnumerable() where r.Field<int>("MasterMatID") == Convert.ToInt32(lblMaterialid.Text) select r).ToList();
            foreach (DataRow dr in result)
            { 
            txtQty.Text=dr["BOMQty"].ToString();
            rowcheck.Checked = true;
            }
        }
        GridDataItem item = e.Item as GridDataItem;
        GridColumn column = gridmaterial.MasterTableView.GetColumn("MasterMatId");
        /*CheckBox rowcheck = (CheckBox)item.FindControl("chbxmatrow");
        Label lblMaterialid = (Label)item.FindControl("lblMaterialid");
        Label lblMaterialDesc = (Label)item.FindControl("lblMaterialDesc");
        TextBox txtQty = (TextBox)item.FindControl("txtQty");*/


       // int MatID = Convert.ToInt32(item.GetDataKeyValue("MasterMatId"));
       // var result = (from r in MapppedItem.AsEnumerable() where r.Field<int>("MasterMatID") == Convert.ToInt32(MatID) select r).ToList();
        // foreach (DataRow dr in result)
        // {
        //     txtQty.Text = dr["BOMQty"].ToString();
        //     rowcheck.Checked = true;
        // }
    }

正在生成Null异常。

1 个答案:

答案 0 :(得分:0)

请尝试使用以下代码段。

<强> ASPX

<telerik:RadGrid ID="gridmaterial" runat="server" AutoGenerateColumns="false" AllowSorting="false" OnColumnCreated="gridmaterial_OnColumnCreated" OnItemDataBound="gridmaterial_OnItemDataBound">
    <MasterTableView AutoGenerateColumns="false" ShowHeadersWhenNoRecords="true" DataKeyNames="MasterMatId">
        <Columns>
            <telerik:GridTemplateColumn UniqueName="RowSelect" AllowFiltering="false">
                <HeaderStyle Width="5%" />
                <ItemTemplate>
                    <asp:CheckBox runat="server" ID="chbxmatrow" ClientIDMode="Static" ViewStateMode="Enabled" />
                </ItemTemplate>
            </telerik:GridTemplateColumn>
            <telerik:GridTemplateColumn HeaderText="#" UniqueName="SerialNumber" AllowFiltering="false">
                <HeaderStyle Width="5%" />
                <ItemTemplate>
                    <asp:Label runat="server" ID="lblSNo" CssClass="control-label" ClientIDMode="Static" Text='<%# Container.DataSetIndex + 1 %>'></asp:Label>
                </ItemTemplate>
            </telerik:GridTemplateColumn>
            <telerik:GridTemplateColumn HeaderText="Group" UniqueName="Group">
                <HeaderStyle Width="20%" />
                <ItemTemplate>
                    <asp:Label runat="server" ID="lblMaterialGroup" CssClass="control-label" ClientIDMode="Static" Text='<%# Eval("Group") %>'></asp:Label>
                </ItemTemplate>
            </telerik:GridTemplateColumn>
            <telerik:GridTemplateColumn HeaderText="Material Description" UniqueName="MasterMatDesc">
                <HeaderStyle Width="50%" />
                <ItemTemplate>
                    <asp:Label runat="server" ID="lblMaterialDesc" CssClass="control-label" ClientIDMode="Static" Text='<%# Eval("Description") %>'></asp:Label>
                </ItemTemplate>
            </telerik:GridTemplateColumn>
            <telerik:GridTemplateColumn HeaderText="Qty" UniqueName="Qty" AllowFiltering="false">
                <HeaderStyle Width="20%" />
                <ItemTemplate>
                    <asp:TextBox runat="server" CssClass="form-control" ID="txtQty" ViewStateMode="Enabled" Width="100" onKeyPress="return isNumber(event);"></asp:TextBox>
                </ItemTemplate>
            </telerik:GridTemplateColumn>
        </Columns>
    </MasterTableView>
    <ClientSettings>
        <Scrolling AllowScroll="true" ScrollHeight="400px" />
    </ClientSettings>
</telerik:RadGrid>

<强> ASPX.CS

protected void gridmaterial_OnItemDataBound(object sender, GridItemEventArgs e)
{
    if (e.Item is GridDataItem)
    {
        GridDataItem item = e.Item as GridDataItem;
        CheckBox rowcheck = (CheckBox)item.FindControl("chbxmatrow");
        Label lblMaterialDesc = (Label)item.FindControl("lblMaterialDesc");
        TextBox txtQty = (TextBox)item.FindControl("txtQty");
        var result = (from r in MapppedItem.AsEnumerable()
                        where r.Field<int>("MasterMatID") == Convert.ToInt32(item.GetDataKeyValue("MasterMatId").ToString())
                        select r.BOMQty).FirstOrDefault();
        txtQty.Text = result;

    }
}

如果我不理解你的要求,请告诉我。