识别Telerik RadGrid中的选定行数

时间:2017-10-23 02:15:38

标签: c# asp.net webforms telerik telerik-grid

我在表单中使用Telerik的RadGrid控件以及FormView。当我在RadGrid中选择一行时,所选行的详细信息显示在FormView控件中。页面上还有一个按钮,单击该按钮时,将FormView的模式更改为“插入”,但前提是选择了RadGrid中的行。否则应出现标签。不幸的是,我无法将RadGrid中所选项目的数量计算得超过0.这意味着当我单击按钮时,即使在RadGrid中选择了一行,我总是会看到标签文本。

这是RadGrid标记:

<telerik:RadGrid ID="rgInsps" runat="server" DataSourceID="sdsRgInsps" AutoGenerateColumns="False" AllowPaging="True" AllowSorting="True" AllowFilteringByColumn="True" ShowGroupPanel="True" Skin="Default" OnSelectedIndexChanged="rgInsps_SelectedIndexChanged" OnPreRender="rgInsps_PreRender" OnItemCommand="rgInsps_ItemCommand">
    <ClientSettings AllowDragToGroup="True" AllowColumnsReorder="True" ReorderColumnsOnClient="True"></ClientSettings>
    <MasterTableView DataKeyNames="inspDetIdPk" DataSourceID="sdsRgInsps">
        <Columns>
            <telerik:GridButtonColumn ButtonType="LinkButton" CommandName="Select" Text="Select"></telerik:GridButtonColumn>
            <telerik:GridBoundColumn DataField="inspDetIdPk" ReadOnly="True" HeaderText="inspDetIdPk" SortExpression="inspDetIdPk" UniqueName="inspDetIdPk" DataType="System.Int32" FilterControlAltText="Filter inspDetIdPk column"></telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="assntIdFk" HeaderText="assntIdFk" SortExpression="assntIdFk" UniqueName="assntIdFk" DataType="System.Int32" FilterControlAltText="Filter assntIdFk column" HeaderStyle-HorizontalAlign="Center" HeaderStyle-Font-Bold="false" ItemStyle-HorizontalAlign="Center"></telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="inspDt" HeaderText="Inspection Date" DataFormatString="{0:MM/dd/yyyy}" SortExpression="inspDt" UniqueName="inspDt" DataType="System.DateTime" FilterControlAltText="Filter inspDt column" HeaderStyle-HorizontalAlign="Center" HeaderStyle-Font-Bold="false" ItemStyle-HorizontalAlign="Center"></telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="inspContDt" HeaderText="Contact Date" DataFormatString="{0:MM/dd/yyyy}" SortExpression="inspContDt" UniqueName="inspContDt" DataType="System.DateTime" FilterControlAltText="Filter inspContDt column" HeaderStyle-HorizontalAlign="Center" HeaderStyle-Font-Bold="false" ItemStyle-HorizontalAlign="Center"></telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="contType" HeaderText="Contact Type" SortExpression="contType" UniqueName="contType" FilterControlAltText="Filter contType column" HeaderStyle-HorizontalAlign="Center" HeaderStyle-Font-Bold="false" ItemStyle-HorizontalAlign="Center"></telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="inspContFN" HeaderText="First Name" SortExpression="inspContFN" UniqueName="inspContFN" FilterControlAltText="Filter inspContFN column" HeaderStyle-HorizontalAlign="Center" HeaderStyle-Font-Bold="false" ItemStyle-HorizontalAlign="Center"></telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="inspContLN" HeaderText="Last Name" SortExpression="inspContLN" UniqueName="inspContLN" FilterControlAltText="Filter inspContLN column" HeaderStyle-HorizontalAlign="Center" HeaderStyle-Font-Bold="false" ItemStyle-HorizontalAlign="Center"></telerik:GridBoundColumn>
        </Columns>
    </MasterTableView>
</telerik:RadGrid> 

这是FormView标记:

<asp:FormView ID="fvInspsDets" 
runat="server" 
DataKeyNames="inspDetIdPk" 
DataSourceID="sdsFvInspDets"
OnItemCommand="fvInspsDets_ItemCommand" 
OnItemUpdated="fvInspsDets_ItemUpdated" 
OnItemInserted="fvInspsDets_ItemInserted" 
OnItemDeleted="fvInspsDets_ItemDeleted">
<EditItemTemplate>
    <asp:Label Text='<%# Eval("inspDetIdPk") %>' runat="server" ID="inspDetIdPkLabel1" Visible="false" />
    <asp:TextBox Text='<%# Bind("assntIdFk") %>' runat="server" ID="assntIdFkTextBox" Visible="false" />
    <asp:TextBox Text='<%# Bind("inspDetUpldBy") %>' runat="server" ID="inspDetUpldByTextBox" Visible="false" />

    <div class="row">
        <div class="col-xs-12 col-sm-6 col-md-3 col-lg-3 form-group">
            <strong>Contact Date & Time</strong><br />
            <asp:TextBox ID="inspContDtTextBox" runat="server" Text='<%# Bind("inspContDt", "{0:MM/dd/yyyy}") %>' CssClass="form-control"></asp:TextBox>
            <ajaxToolkit:CalendarExtender ID="cdInspContDtTextBox" runat="server" TargetControlID="inspContDtTextBox" Format="MM/dd/yyyy" />
        </div>
        <div class="col-xs-12 col-sm-6 col-md-3 col-lg-3 form-group">
            <strong>Contact Type</strong><br />
            <asp:DropDownList SelectedValue='<%# Bind("inspContType") %>' runat="server" ID="inspContTypeDdl" CssClass="form-control" AppendDataBoundItems="true" DataSourceID="sdsContTypes" DataTextField="contType" DataValueField="contTypeIdPk"><asp:ListItem Value="">--Please Select--</asp:ListItem></asp:DropDownList>
        </div>
        <div class="col-xs-12 col-sm-6 col-md-3 col-lg-3 form-group">
            <strong>Contact's First Name</strong><br />
            <asp:TextBox Text='<%# Bind("inspContFN") %>' runat="server" ID="inspContFNTextBox" CssClass="form-control" />
        </div>
        <div class="col-xs-12 col-sm-6 col-md-3 col-lg-3 form-group">
            <strong>Contact's Last Name</strong><br />
            <asp:TextBox Text='<%# Bind("inspContLN") %>' runat="server" ID="inspContLNTextBox" CssClass="form-control" />
        </div>
        <div class="col-xs-12 col-sm-6 col-md-3 col-lg-3 form-group">
            <strong>Inspection Date & Time</strong><br />
            <asp:TextBox ID="inspDtTextBox" runat="server" Text='<%# Bind("inspDt", "{0:MM/dd/yyyy}") %>' CssClass="form-control"></asp:TextBox>
            <ajaxToolkit:CalendarExtender ID="ceInspDtTextBox" runat="server" TargetControlID="inspDtTextBox" Format="MM/dd/yyyy" />
        </div>
        <div class="col-xs-12 col-sm-6 col-md-3 col-lg-3 form-group">
            <strong>Inspection Rescheduled</strong><br />
            <asp:CheckBox Checked='<%# Bind("inspResched") %>' runat="server" ID="inspReschedCheckBox" />
        </div>
    </div>
    <div class="row">
        <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12 form-group">
            <strong>Note(s)</strong><br />
            <asp:TextBox Text='<%# Bind("inspNote") %>' runat="server" ID="inspNoteTextBox" CssClass="form-control" Rows="3" TextMode="MultiLine" />
        </div>
    </div>
    <asp:LinkButton runat="server" Text="Update" CommandName="Update" ID="UpdateButton" CausesValidation="True" CssClass="btn btn-success btn-sm" />&nbsp;
    <asp:LinkButton runat="server" Text="Cancel" CommandName="Cancel" ID="UpdateCancelButton" CausesValidation="False" CssClass="btn btn-danger btn-sm" />
</EditItemTemplate>
<InsertItemTemplate>
    <asp:TextBox Text='<%# Bind("assntIdFk") %>' runat="server" ID="assntIdFkTextBox" Visible="false" />
    <asp:TextBox Text='<%# Bind("inspDetUpldBy") %>' runat="server" ID="inspDetUpldByTextBox" Visible="false" />

    <div class="row">
        <div class="col-xs-12 col-sm-6 col-md-3 col-lg-3 form-group">
            <strong>Contact Date & Time</strong><br />
            <telerik:RadDateTimePicker ID="rdtpInspContDt" runat="server" DbSelectedDate='<%# Bind("inspContDt") %>' Skin="Default" CssClass="form-control" ></telerik:RadDateTimePicker>
        </div>
        <div class="col-xs-12 col-sm-6 col-md-3 col-lg-3 form-group">
            <strong>Contact Type</strong><br />
            <asp:DropDownList SelectedValue='<%# Bind("inspContType") %>' runat="server" ID="inspContTypeDdl" CssClass="form-control" AppendDataBoundItems="true" DataSourceID="sdsContTypes" DataTextField="contType" DataValueField="contTypeIdPk"><asp:ListItem Value="">--Please Select--</asp:ListItem></asp:DropDownList>
        </div>
        <div class="col-xs-12 col-sm-6 col-md-3 col-lg-3 form-group">
            <strong>Contact's First Name</strong><br />
            <asp:TextBox Text='<%# Bind("inspContFN") %>' runat="server" ID="inspContFNTextBox" CssClass="form-control" />
        </div>
        <div class="col-xs-12 col-sm-6 col-md-3 col-lg-3 form-group">
            <strong>Contact's Last Name</strong><br />
            <asp:TextBox Text='<%# Bind("inspContLN") %>' runat="server" ID="inspContLNTextBox" CssClass="form-control" />
        </div>
        <div class="col-xs-12 col-sm-6 col-md-3 col-lg-3 form-group">
            <strong>Inspection Date & Time</strong><br />
            <telerik:RadDateTimePicker ID="rdtpInspDt" runat="server" DbSelectedDate='<%# Bind("inspDt") %>' CssClass="form-control"></telerik:RadDateTimePicker>
        </div>
        <div class="col-xs-12 col-sm-6 col-md-3 col-lg-3 form-group">
            <strong>Inspection Rescheduled</strong><br />
            <asp:CheckBox Checked='<%# Bind("inspResched") %>' runat="server" ID="inspReschedCheckBox" />
        </div>
    </div>
    <div class="row">
        <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12 form-group">
            <strong>Note(s)</strong><br />
            <asp:TextBox Text='<%# Bind("inspNote") %>' runat="server" ID="inspNoteTextBox" CssClass="form-control" Rows="3" TextMode="MultiLine" />
        </div>
    </div>                            
    <asp:LinkButton runat="server" Text="Insert" CommandName="Insert" ID="InsertButton" CausesValidation="True" CssClass="btn btn-success btn-sm" />&nbsp;
    <asp:LinkButton runat="server" Text="Cancel" CommandName="Cancel" ID="InsertCancelButton" CausesValidation="False" CssClass="btn btn-danger btn-sm" />
</InsertItemTemplate>
<ItemTemplate>
    <asp:Label Text='<%# Eval("inspDetIdPk") %>' runat="server" ID="inspDetIdPkLabel" Visible="false" />
    <asp:Label Text='<%# Bind("assntIdFk") %>' runat="server" ID="assntIdFkLabel" Visible="false" />
    <asp:Label Text='<%# Bind("inspDetUpldBy") %>' runat="server" ID="inspDetUpldByLabel" Visible="false" />

    <div class="row">
        <div class="col-xs-12 col-sm-6 col-md-3 col-lg-3 form-group">
            <strong>Contact Date & Time</strong><br />
            <asp:Label Text='<%# Bind("inspContDt") %>' runat="server" ID="inspContDtLabel" CssClass="form-control" />
        </div>
        <div class="col-xs-12 col-sm-6 col-md-3 col-lg-3 form-group">
            <strong>Contact Type</strong><br />
            <asp:Label Text='<%# Bind("contType") %>' runat="server" ID="inspContTypeLabel" CssClass="form-control" />
        </div>
        <div class="col-xs-12 col-sm-6 col-md-3 col-lg-3 form-group">
            <strong>Contact's First Name</strong><br />
            <asp:Label Text='<%# Bind("inspContFN") %>' runat="server" ID="inspContFNLabel" CssClass="form-control" />
        </div>
        <div class="col-xs-12 col-sm-6 col-md-3 col-lg-3 form-group">
            <strong>Contact's Last Name</strong><br />
            <asp:Label Text='<%# Bind("inspContLN") %>' runat="server" ID="inspContLNLabel" CssClass="form-control" />
        </div>
        <div class="col-xs-12 col-sm-6 col-md-3 col-lg-3 form-group">
            <strong>Inspection Date & Time</strong><br />
            <asp:Label Text='<%# Bind("inspDt") %>' runat="server" ID="inspDtLabel" CssClass="form-control" />
        </div>
        <div class="col-xs-12 col-sm-6 col-md-3 col-lg-3 form-group">
            <strong>Rescheduled</strong><br />
            <asp:CheckBox Checked='<%# Bind("inspResched") %>' runat="server" ID="inspReschedCheckBox" Enabled="false" />
        </div>
    </div>
    <div class="row">
        <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12 form-group">
            <strong>Note(s)</strong><br />
            <asp:Label Text='<%# Bind("inspNote") %>' runat="server" ID="inspNoteLabel" />
        </div>
    </div>
    <asp:LinkButton runat="server" Text="Edit" CommandName="Edit" ID="EditButton" CausesValidation="False" CssClass="btn btn-primary btn-sm" />&nbsp;
    <asp:LinkButton runat="server" Text="Delete" CommandName="Delete" ID="DeleteButton" CausesValidation="False" CssClass="btn btn-danger btn-sm" />&nbsp;
    <asp:LinkButton runat="server" Text="New" CommandName="New" ID="NewButton" CausesValidation="False" CssClass="btn btn-success btn-sm" />
</ItemTemplate>
</asp:FormView>

这是标签:

<asp:Label ID="fvInspsDetsLbl" runat="server"></asp:Label>

这是按钮:

<asp:Button ID="btnRvInspsDetsInsert" 
runat="server" 
Text="ADD A NEW INSPECTION" 
CssClass="btn btn-default btn-sm pull-right" 
OnClick="btnRvInspsDetsInsert_Click" 
CausesValidation="false" />

这是按钮的点击事件代码:

if (rgAssnmtList.SelectedItems.Count > 0)
{
    fvInspsDets.ChangeMode(FormViewMode.Insert);
}
else
{
    fvInspsDetsLbl.Text = "Please select an assignment from the table at the top of the screen.";
    fvInspsDetsLbl.ForeColor = System.Drawing.ColorTranslator.FromHtml("#CA2823");
}

非常感谢任何有关识别RadGrid所选项目数的帮助。

我正在使用VS2017,Asp.NET WebForms C#以及Telerik产品(因此是RadGrid)。

1 个答案:

答案 0 :(得分:1)

使用radGridView1.SelectedCells.Count获取所选单元格,或radGridView1.SelectedRows.Count

还可以使用RowInfoColumnInfo来访问所选的rowcolumn

检查此示例代码:

this.radGridView1.MasterTemplate.BeginUpdate();
int count = this.radGridView1.SelectedCells.Count;
for (int i = 0; i < count; i++)
{
    GridViewRowInfo row = this.radGridView1.SelectedCells[i].RowInfo;
    GridViewColumn column = this.radGridView1.SelectedCells[i].ColumnInfo;

    string text = Convert.ToString(this.radGridView1.SelectedCells[i].ColumnInfo.HeaderText);
    GridViewCellInfo cell = this.radGridView1.SelectedCells[i]; 
    double val = Convert.ToDouble(cell.Value);
}
this.radGridView1.MasterTemplate.EndUpdate();