Many thanks. I've put this shown below and the key name is based on the item also shown below but I've got the following error (in italic)
DataKeyNames="itm_start_dt"
Many thanks. I used one existing column name from that Selectcommand and now there're no problem. But the issue is nothing will happen when I click the 'New' button which is with this event
protected void gv_ins(object sender, GridViewRowEventArgs e)
{
int tot = gv1.Rows.Count;
for (int r = 0; r < tot; r++)
{
if (gv1.DataKeys[r].Values[0].ToString() == "")
{
gv1.EditIndex = r;
break;
}
}
}
Many thanks. I did click the 'New' button of the gridview to insert one new record. How to use rowcommand event handler to handle the custom insert command?
Take note on e.CommandName == "InsertNew". The CommandName is what you set for the button.
E.g.
<asp:Button Id="btnNew" runat="server" CommandName="InsertNew" />
Many thanks Kpyap. I have changed the event to be
protected void gv_ins(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "InsertNew")
ds_rec_det.Insert();
}
where ds_rec_det is the data source name of the gridview
I've also removed this in the Markup
Onrowcreated="gv_ins"
but nothing will happen when I've clicked the 'New' button of the gridview
kpyap
Contributor
5212 Points
989 Posts
Re: Onrowcreated
Oct 12, 2009 07:48 AM|LINK
Hi,
Sample code:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="au_id" DataSourceID="SqlDataSource1">
If you have composite key columns, you can set which comma, like DataKeyNames="au_id, book_id"
Hope it help
wmec
Contributor
6195 Points
3214 Posts
Re: Onrowcreated
Oct 12, 2009 08:19 AM|LINK
Many thanks. I've put this shown below and the key name is based on the item also shown below but I've got the following error (in italic)
DataKeyNames="itm_start_dt"
<asp:TemplateField HeaderText="Start Date" HeaderStyle-HorizontalAlign="Left" >
<ItemTemplate>
<asp:TextBox runat="server" ID="itm_start_dt" HtmlEncode="False" ForeColor="DarkBlue" BackColor="AntiqueWhite" BorderStyle="None"
ReadOnly="true" Width="65px" Text='<%# Eval("start_dt") %>'/>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="edi_start_dt" HtmlEncode="False"
Enabled="true" Width="65px" Text='<%# Eval("start_dt") %>' />
</EditItemTemplate>
</asp:TemplateField>
Server Error in '/' Application.
--------------------------------------------------------------------------------
DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'chk_start_dt2'.
HuaMin Chen
kpyap
Contributor
5212 Points
989 Posts
Re: Onrowcreated
Oct 12, 2009 08:39 AM|LINK
Hi,
From the error message, looks like you are trying to bind the datasource of column named chk_start_dt2 which does not exists.
Please check your SQL and also control that bind to chk_start_dt2.
wmec
Contributor
6195 Points
3214 Posts
Re: Onrowcreated
Oct 12, 2009 09:18 AM|LINK
Many thanks. I used one existing column name from that Selectcommand and now there're no problem. But the issue is nothing will happen when I click the 'New' button which is with this event
protected void gv_ins(object sender, GridViewRowEventArgs e)
{
int tot = gv1.Rows.Count;
for (int r = 0; r < tot; r++)
{
if (gv1.DataKeys[r].Values[0].ToString() == "")
{
gv1.EditIndex = r;
break;
}
}
}
HuaMin Chen
kpyap
Contributor
5212 Points
989 Posts
Re: Onrowcreated
Oct 13, 2009 01:20 AM|LINK
Hi,
When you debug, will it step into gv.EditIndex = r; ?
So far I know, GridView has no Insert command built on it.
I think you should use RowCommand event handler to handle your custom Insert command.
Not sure link below is what you look for
http://www.aspdotnetfaq.com/Faq/How-to-insert-row-in-GridView-with-SqlDataSource.aspx
Hope it help
wmec
Contributor
6195 Points
3214 Posts
Re: Onrowcreated
Oct 13, 2009 01:45 AM|LINK
Many thanks. I did click the 'New' button of the gridview to insert one new record. How to use rowcommand event handler to handle the custom insert command?
HuaMin Chen
kpyap
Contributor
5212 Points
989 Posts
Re: Onrowcreated
Oct 13, 2009 02:25 AM|LINK
Hi,
I cut some code from the link I posted previously
protected void Customers_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName == "InsertNew") { TextBox FirstName = Customers.FooterRow.FindControl("InsertFirstName") as TextBox; TextBox LastName = Customers.FooterRow.FindControl("InsertLastName") as TextBox; SqlParameter fName = new SqlParameter("@FirstName", SqlDbType.VarChar, 30); fName.Direction = ParameterDirection.Input; fName.Value = FirstName.Text; insertParameters.Add(fName); SqlParameter lName = new SqlParameter("@LastName", SqlDbType.VarChar, 30); lName.Direction = ParameterDirection.Input; lName.Value = LastName.Text; insertParameters.Add(lName); CustomersSqlDataSource.Insert(); } }Take note on e.CommandName == "InsertNew". The CommandName is what you set for the button.
E.g.
<asp:Button Id="btnNew" runat="server" CommandName="InsertNew" />
Hope it help.
wmec
Contributor
6195 Points
3214 Posts
Re: Onrowcreated
Oct 13, 2009 04:24 AM|LINK
Many thanks Kpyap. I have changed the event to be
protected void gv_ins(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "InsertNew")
ds_rec_det.Insert();
}
where ds_rec_det is the data source name of the gridview
I've also removed this in the Markup
Onrowcreated="gv_ins"
but nothing will happen when I've clicked the 'New' button of the gridview
HuaMin Chen
kpyap
Contributor
5212 Points
989 Posts
Re: Onrowcreated
Oct 13, 2009 05:19 AM|LINK
Hi,
When debug, is the event handler been executed?
Has you bind the event?
E.g.
<asp:gridview id="gv" datasourceid="ds_rec" onrowcommand="gv_ins" runat="server">
Hope it help.
wmec
Contributor
6195 Points
3214 Posts
Re: Onrowcreated
Oct 13, 2009 06:24 AM|LINK
Many many thanks. Yes, I've put this
OnRowCommand="gv_ins"
The event was fired but the condition can't be satisfied
if (e.CommandName == "InsertNew")
HuaMin Chen