在gridview中获取单元格值

时间:2012-06-08 13:48:06

标签: .net gridview

我想从gridview中的文字中获取文本,

但是当我运行程序时,异常通过

无法将System.Web.UI.LiteralControl类型的对象强制转换为System.Web.UI.DataBoundLiteralControl类型。

.aspx代码:

<asp:GridView ID="gridview3" runat="server" OnRowDataBound="RowDataBound" DataKeyNames="qno" AutoGenerateColumns="false"  ShowFooter="true" OnRowCancelingEdit="cancel"   OnRowCommand="create" OnRowDeleting="delete" OnRowEditing="edit" OnRowUpdating="update">

            <Columns>
            <asp:TemplateField HeaderText="Selection">
            <ItemTemplate>
                <asp:CheckBox ID="check1" runat="server"/>
            </ItemTemplate>

            </asp:TemplateField>
             <asp:TemplateField HeaderText="ID" Visible="true">
        <ItemTemplate>
            <asp:Label ID="id7" runat="server" Text='<%#Eval("assessid") %>' ></asp:Label>

        </ItemTemplate>
        </asp:TemplateField>

    <asp:TemplateField HeaderText="Qno" Visible="true">
        <ItemTemplate>
         <asp:DropDownList  AppendDataBoundItems="true"  AutoPostBack="true"     ID="DropDownList1"  runat="server">
         <asp:ListItem ></asp:ListItem>


        </asp:DropDownList>

        </ItemTemplate>



    </asp:TemplateField>




      <asp:TemplateField HeaderText="description" Visible="true">
        <ItemTemplate>

         <asp:Literal ID="id6" runat="server" Text='<%#Eval("description") %>' >
         </asp:Literal>
        </ItemTemplate>
         <EditItemTemplate>
             <asp:TextBox ID="TextBox1" Text='<%#Eval("description") %>' runat="server" ></asp:TextBox>

        </EditItemTemplate>
        <FooterTemplate>

        <asp:TextBox ID="TextBox3"  runat="server" ></asp:TextBox>




        </FooterTemplate>


    </asp:TemplateField>











     <asp:TemplateField HeaderText="strongagree" Visible="true">
        <EditItemTemplate>
        =

      <asp:TemplateField HeaderText="Action" Visible="true">

        <ItemTemplate>
            <asp:LinkButton ID="LinkButton5" Text="Edit" CommandName="edit" runat="server"></asp:LinkButton>
    </ItemTemplate>
    <EditItemTemplate>

       <asp:LinkButton ID="LinkButton1" Text="update" CommandName="update" runat="server"></asp:LinkButton>

       <asp:LinkButton ID="LinkButton3" Text="cancel" CommandName="cancel" runat="server"></asp:LinkButton>
    </EditItemTemplate>
    <FooterTemplate>

    <asp:LinkButton ID="LinkButton7" Text="DeleteAll"  CommandName="delete" runat="server"></asp:LinkButton>

    </FooterTemplate>
</asp:TemplateField>

    <asp:TemplateField HeaderText="" Visible="true">

        <ItemTemplate>

          <asp:LinkButton ID="LinkButton23" Text="delete" CommandName="delete" runat="server"></asp:LinkButton>




    </ItemTemplate>

    <FooterTemplate>
    <asp:Button ID="bdh" Text="insert " CommandName="insert" runat="server" />

    </FooterTemplate>

</asp:TemplateField>







</Columns>






            <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
            <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
            <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
            <SortedAscendingCellStyle BackColor="#FDF5AC" />
            <SortedAscendingHeaderStyle BackColor="#4D0000" />
            <SortedDescendingCellStyle BackColor="#FCF6C0" />
            <SortedDescendingHeaderStyle BackColor="#820000" />




        </asp:GridView>

代码

protected void RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
       string percentage = ((LiteralControl)e.Row.Cells[2].Controls[0]).Text;
    }
}

2 个答案:

答案 0 :(得分:2)

你要回的文字控件可能不是你想要的那个(可能有一个空格的文字匹配)。试着通过id获取它:

string percentage = ((LiteralControl)e.Row.FindControl("id6").Text; 

或者尝试另一个索引:

string percentage = ((LiteralControl)e.Row.Cells[2].Controls[1]).Text; 

答案 1 :(得分:0)

它应该是Cells [3],而不是Cells [2],因为你的Literal控件位于第4列。但是不要在那里硬编码索引。如果您想稍后添加更多列,这很痛苦。相反,尝试这样的事情......

protected void RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        Literal id6 = (Literal)e.Row.FindControl("id6");
        string percentage = "";

        if (id6 != null) {
            percentage = id6.Text;
        }
    }
}