0

我使用此代码返回DataTable

public static DataTable ListBooks(this List<classes.Book> objs)
    { 
        DataTable table = new DataTable();

        table.Columns.Add("id");
        table.Columns.Add("name");
        table.Columns.Add("dewey");
        table.Columns.Add("subject");
        table.Columns.Add("reg");
        table.Columns.Add("pub");
        var values = new object[6];
        if (objs != null)
            foreach (classes.Book item in objs)
            {
                values[0] = item.Id;
                values[1] = item.Name;
                values[2] = item.Dewey;
                values[3] = item.Subject;
                values[4] = IntToDateTime(item.RegDate);
                if (item.PubDate != null)
                    values[5] = IntToDateTime(item.PubDate);
                else
                    values[5] = "";
                table.Rows.Add(values);
            }
        return table;
    }

以及将此代码提供给GridView

    protected void Page_Load(object sender, EventArgs e)
    {
        DataTable dt = Converter.ListBooks(new classes.Book().GetAll());
        Session["dt"] = dt;
        res.DataSource = dt;
        res.DataBind();

    }

aspx

<asp:GridView ID="res" runat="server" CellPadding="4" ForeColor="#333333" 
    GridLines="None" AutoGenerateColumns="False" Width="965px" 
    OnRowEditing="res_RowEditing"  OnRowCancelingEdit="res_RowCancelingEdit" 
    OnRowUpdating="res_RowUpdating"
    OnPageIndexChanging="res_PageIndexChanging" AllowPaging="True" >
    <AlternatingRowStyle BackColor="White" />
    <Columns>
        <asp:CommandField DeleteText="del" HeaderText="           " 
            ShowDeleteButton="True" />
        <asp:CommandField EditText="edit" HeaderText="                             " 
            ShowEditButton="True" />
        <asp:BoundField DataField="reg" HeaderText="reg date" ReadOnly="true">
        <HeaderStyle Wrap="False" />
        </asp:BoundField>
        <asp:BoundField DataField="pub" HeaderText="pub date" ReadOnly="true"/>
        <asp:BoundField DataField="subject" HeaderText="subject" />
        <asp:BoundField DataField="dewey" HeaderText="dewey" />
        <asp:BoundField DataField="name" HeaderText="title" />
    </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" HorizontalAlign="Center" />
    <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
    <SortedAscendingCellStyle BackColor="#FDF5AC" />
    <SortedAscendingHeaderStyle BackColor="#4D0000" />
    <SortedDescendingCellStyle BackColor="#FCF6C0" />
    <SortedDescendingHeaderStyle BackColor="#820000" />
</asp:GridView>

以及此行更新代码:

    protected void res_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        //Retrieve the table from the session object.
        DataTable dt = (DataTable)Session["dt"];
        GridViewRow row =res.Rows[e.RowIndex];
        res.Visible = false;
        *dt.Rows[row.DataItemIndex]["name"] = ((TextBox)(row.Cells[1].Controls[0])).Text;
        dt.Rows[row.DataItemIndex]["dewey"] = ((TextBox)(row.Cells[2].Controls[0])).Text;
        dt.Rows[row.DataItemIndex]["subject"] = ((TextBox)(row.Cells[3].Controls[0])).Text;
        res.DataBind();
    }

但是在更新行时,此错误发生在上面带有 * 的代码中:

无法将“System.Web.UI.WebControls.DataControlLinkBut​​ton”类型的对象转换为“System.Web.UI.WebControls.TextBox”类型。

4

2 回答 2

1

Cells的索引是错误的;因为当您要访问Cells第一个索引时包含编辑和删除LableButton,所以:

 dt.Rows[row.DataItemIndex]["name"] = ((TextBox)(row.Cells[6].Controls[0])).Text
 dt.Rows[row.DataItemIndex]["dewey"] = ((TextBox)(row.Cells[5].Controls[0])).Text;
 dt.Rows[row.DataItemIndex]["subject"] = ((TextBox)(row.Cells[4].Controls[0])).Text;
于 2013-04-22T05:48:06.597 回答
0

尝试这个:

 {
    DataTable dt = (DataTable)Session["dt"];
    GridViewRow row =res.Rows[e.RowIndex];
    res.Visible = false;
    dt.Rows[row.DataItemIndex]["name"] = ((LinkButton)(row.Cells[1].Controls[0])).Text;
    dt.Rows[row.DataItemIndex]["dewey"] = ((TextBox)(row.Cells[2].Controls[0])).Text;
    dt.Rows[row.DataItemIndex]["subject"] = ((TextBox)(row.Cells[3].Controls[0])).Text;
    res.DataBind();
}
于 2013-04-22T05:16:19.273 回答