Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > EditItemIndex not working when adding new records with datagrid

Reply
Thread Tools

EditItemIndex not working when adding new records with datagrid

 
 
tafs7
Guest
Posts: n/a
 
      09-11-2003
I have a datagrid in which I added a linkbutton (to the footer of my
only template column) that raises the OnClick event and fires the
AddNew method (see code below). I'm trying to add a new record to the
database by setting all its fields to a value of 0, then I need to get
to the edit mode of the data grid, for that specific record and be
able to edit its values from the initialized zeroes, to whatever
they'll be.

The event fires, and the new record is inserted into the database just
fine. The problem is that the datagrid never goes into edit mode so
that I can edit the newly added record. The user has to manually page
to the new record (which is the last page of the grid), then click on
the edit command from the EditCommandColumn.

How can I automatically go into editmode after adding the new record?

I know the edit mode will work if I am doing updates to an existing
record, that's why I was inserting a new "blank" record to the db,
then trying to edit that record. Please would somebody help. Is there
a better way of doing this, the adding new records with datagrid,
etc.?

regards,
Thiago Silva
web developer
AgniTEK

Below is my code for the AddNew event and the datagrid's
editcommandcolumn, plus the footertemplate that contains the
LinkButton AddNew that raises the event:
<aspataGrid id=dgClientInfo runat="server" ShowFooter="True"
AutoGenerateColumns="False"
GridLines="None"
Cellpadding="5"
BackColor="LightBlue"
BorderColor="Navy"
BorderStyle="Dashed"
BorderWidth="1px"
CssClass="setText"
AllowPaging="True"
OnPageIndexChanged="dgClientInfo_Page"
PageSize="1"

OnCancelCommand="dgClientInfo_Cancel"
OnUpdateCommand="dgClientInfo_Update"
OnDeleteCommand="dgClientInfo_Delete"
OnEditCommand="dgClientInfo_Edit"

HeaderStyle-BackColor="lightgrey"
HeaderStyle-Font-Name="Verdana"
HeaderStyle-Font-Bold="True"
HeaderStyle-HorizontalAlign="Right"

FooterStyle-BackColor="lightgrey"
FooterStyle-Font-Name="Verdana"
FooterStyle-Font-Bold="True" >
<Columns>
<asp:EditCommandColumn EditText=" Edit "
CancelText=" Cancel "
UpdateText=" Save "
ItemStyle-Wrap="True"
ItemStyle-Width="15"
ItemStyle-VerticalAlign="Top"/>
(...)

</EditItemTemplate>
<FooterTemplate>
<asp:LinkButton Runat="server"
Text="Add New Record"
OnClick="AddNew"
ID="LinkButton1"
Name="LinkButton1" />
| <asp:LinkButton Runat="server"
Text="View All Records"
OnClick = "ClearSearch"
id="LinkButton2"
name="LinkButton2" />
</FooterTemplate>
</asp:TemplateColumn>
</Columns>

(...)
void AddNew(Object source, EventArgs e)
{
string s = "SELECT * FROM tblTAIP";
DataSet newDS = GetDS(s);
DataTable table = newDS.Tables[0];
DataRow newrow = table.NewRow();
foreach(DataColumn dc in table.Columns)
{
if(dc.DataType==System.Type.GetType("System.Int32" ))
newrow[dc] = 0;
else
newrow[dc] = "0";
}

table.Rows.Add(newrow);

//Connection String to the database
string strConn = ConfigurationSettings.AppSettings["DBConnectionString"];

//sql statement to get the datagrid binding table
string strDGSQL = "SELECT * FROM tblTAIP";

SqlDataAdapter adapter = new SqlDataAdapter(strDGSQL, strConn);
SqlCommandBuilder builder = new SqlCommandBuilder(adapter);
adapter.Update(table);

dgClientInfo.DataSource = newDS;
dgClientInfo.DataBind();

//find the last record in the datagrid
// which is the only one in the page (pagesize=1)

dgClientInfo.EditItemIndex = dgClientInfo.PageCount-1;
dgClientInfo.DataSource = newDS;
dgClientInfo.DataBind();

}
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Datagrid.EditItemIndex Puzzle Just Me ASP .Net 0 08-30-2007 09:06 PM
Delete records or update records Dan ASP General 1 05-10-2004 01:25 PM
match muliple header records to associated detail records Luke Airig XML 0 12-31-2003 12:06 AM
EditItemIndex not working when adding new records with datagrid tafs7 ASP .Net Datagrid Control 0 09-11-2003 05:38 PM
Smartnavigation and EditItemIndex problem karim ASP .Net 0 07-16-2003 03:47 PM



Advertisments