Reputation: 14253
I use this code to return 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;
}
and this code for giving that to 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>
and this code for row update:
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();
}
but in updating row this error occurs in line that in shown in above code with *:
Unable to cast object of type 'System.Web.UI.WebControls.DataControlLinkButton' to type 'System.Web.UI.WebControls.TextBox'.
Upvotes: 0
Views: 2555
Reputation: 14253
Cells
's indexes are wrong; because when you want to access to Cells
first indexes are contain edit and delete LableButton
so :
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;
Upvotes: 1
Reputation: 2111
Try this:
{
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();
}
Upvotes: 0