0

我想从每个记录的网格视图中获取我当前的老鼠并将其保存在我的数据库中。但我不能像网格视图中的其他控件一样调用它。如何在网格视图中保存每一行的费率?这是我在数据库表中的记录 ID:

 int idx = Convert.ToInt32(e.CommandArgument);

    idx = idx - (GridView2.PageSize * GridView2.PageIndex);

    int ID = (int)GridView2.DataKeys[idx].Value;

我每一行都需要这个:

int rating = rating2.CurrentRating;

用于gridview的HTML代码:

<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" 
                DataKeyNames="ID" DataSourceID="SqlDataSource1" Font-Names="Tahoma" 
                Font-Size="9pt" style="margin-left: 0px" Width="799px" GridLines="None">
                <Columns>
                    <asp:TemplateField>
                        <ItemTemplate>
                            <div style="width: 950px">
                                <table align="center" style="width: 100%;">
                                    <tr>
                                        <td align="center" colspan="3">
                                            <div class="box1">
                                                <div class="box-t">
                                                    <div class="b-l">
                                                    </div>
                                                    <div class="b-bg" dir="rtl">
                                                        <asp:Label ID="Label6" runat="server" Font-Names="Tahoma" 
                                                            Text='<%# Eval("Title") %>'></asp:Label>
                                                    </div>
                                                    <div class="b-r">
                                                    </div>
                                                </div>
                                                <div class="box-bg">
                                                    <div class="content">
                                                        <table style="width: 100%; height: 8px;">
                                                            <tr>
                                                                <td align="right">
                                                                    <div style="padding: 15px">
                                                                        <asp:Label ID="Label10" runat="server" Font-Names="Tahoma" ForeColor="Black" 
                                                                            Text='<%# Eval("Body") %>'></asp:Label>
                                                                    </div>
                                                                    <br />
                                                                </td>
                                                            </tr>
                                                            <tr>
                                                                <td align="right">

                                                                    <br />

                                                                    <asp:Rating ID="Rating2" runat="server" CurrentRating="1" MaxRating="6" StarCssClass="ratingStar"
    WaitingStarCssClass="savedRatingStar" FilledStarCssClass="filledRatingStar" EmptyStarCssClass="emptyRatingStar" style="float: right;" />
                                                                </td>
                                                            </tr>
                                                            <tr>
                                                                <td align="right">
                                                                    <asp:Button ID="btnSubmit" runat="server" 
                                                                        CommandArgument="<%# Container.DataItemIndex %>" Font-Names="Tahoma" 
                                                                        Font-Size="9pt" oncommand="SubmitRating" Text="&#1576;&#1607; &#1575;&#1740;&#1606; &#1605;&#1591;&#1604;&#1576; &#1575;&#1605;&#1578;&#1740;&#1575;&#1586; &#1583;&#1607;&#1740;&#1583;" />
                                                                </td>
                                                            </tr>
                                                        </table>
                                                    </div>
                                                    &nbsp;
                                                </div>
                                                <div class="box-b">
                                                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                                    <asp:Label ID="Label8" runat="server" Font-Italic="False" Font-Names="Homa" 
                                                        Text="         &#1578;&#1575;&#1585;&#1740;&#1582; &#1576;&#1575;&#1585;&#1711;&#1584;&#1575;&#1585;&#1740; :"></asp:Label>
                                                    <asp:Label ID="Label11" runat="server" Font-Names="Homa" 
                                                        Text='<%# JulianToPersianDate((DateTime)Eval("Date")) %>'></asp:Label>
                                                </div>
                                            </div>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td align="center">
                                            &nbsp;</td>
                                        <td align="center">
                                            &nbsp;</td>
                                        <td align="center">
                                            &nbsp;</td>
                                    </tr>
                                </table>
                            </div>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>

任何身体都可以帮助我吗?

4

1 回答 1

0

您可以循环浏览您的gridview

foreach(GridViewRow row in GridView2.Rows)
{
    // here you'll get all rows with RowType=DataRow
    // others like Header are omitted in a foreach
}

并访问您的所有列

foreach(GridViewRow row in GridView2.Rows)
{
    for(int i = 0; i < GridView2.Columns.Count, i++)
    {
        //String header = GridView2.Columns[i].HeaderText;
        String cellText = row.Cells[i].Text;
    }
 }

同样,您可以在网格视图中使用查找控件,如下所示

foreach(GridViewRow row in gv.Rows)
{
    TextBox txtTotal = row.cells[0].Controls[0].FindControl("TOTAL") as TextBox;
    string value = txtTotal.Text;

    // Do something with the textBox's value
}

同样,您必须Rating2在单元格中找到您并将其投入评级控制并获得其价值。

于 2013-01-04T11:43:38.690 回答