我使用此代码返回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.DataControlLinkButton”的对象强制转换为“System.Web.UI.WebControls.TextBox”。
答案 0 :(得分: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;
答案 1 :(得分: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();
}