EditItemIndex not working when adding new records with datagrid

T

tafs7

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:
<asp:DataGrid 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();

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top