I'm sorry I took so long to reply but I've been pretty busy over the last few days.
What you need to do is...in the RowCommand event:
- Retrieve the Row that Insert link button that was clicked belongs to. The way that I did this was to bind the CommandArgument for the Insert link button to the Container's (the GidView's) DataItemIndex. This way I was able to retrieve the row based on the index supplied as the CommandArgument in the RowCommand event.
- Retrieve the ID of the item selected.
- Find the Index of the selected item in your data source.
- Insert a new item into your data source at the next index.
- Set the GridView's RowEditIndex to row under the row that was clicked on.
I tried this out using the testing code based on the code that you posted previously in this thread:
How to update a database without using SqlDataSource.
Here is the GridView markup. Please note that I changed the ID of "Label6" to "DummyID" to better reflect the value that it is displaying. Also, please note that I have added code that binds the CommandArgument for each LinkButton to the RowIndex of the item in the GridView...and I've also added Paging.
-
<asp:GridView ID="gv_DisplayClient" runat="server" DataKeyNames="DummyID" ForeColor="#333333"
-
GridLines="Horizontal" CellPadding="4" AutoGenerateColumns="false" OnRowEditing="GvEditing"
-
OnRowCancelingEdit="gv_EditCancel" EnableViewState="true" OnRowUpdating="gv_Updating"
-
OnRowDeleting="gv_Deleting" OnRowCommand="gv_DisplayClient_RowCommand" ShowFooter="true"
-
AllowPaging="true" OnPageIndexChanging="gv_DisplayClient_OnPageIndexChanging" PageSize="5">
-
<PagerSettings Mode="NumericFirstLast" Position="Top" />
-
<Columns>
-
<asp:TemplateField HeaderText="ClientId">
-
<ItemTemplate>
-
<asp:Label ID="DummyID" runat="server" Text='<%#Bind("DummyID")%>'></asp:Label>
-
</ItemTemplate>
-
<FooterTemplate>
-
<asp:Label ID="LblInsertPrompt" runat="server" Text="New Dummy:"></asp:Label>
-
</FooterTemplate>
-
</asp:TemplateField>
-
<asp:TemplateField HeaderText="ClientName">
-
<ItemTemplate>
-
<asp:Label ID="lblClientName" runat="server" Text='<%#Eval("ClientName")%>'></asp:Label>
-
</ItemTemplate>
-
<EditItemTemplate>
-
<asp:TextBox ID="gvClientName" runat="server" Text='<%#Bind("ClientName")%>'></asp:TextBox>
-
</EditItemTemplate>
-
<FooterTemplate>
-
<asp:TextBox ID="gvClientName" runat="server"></asp:TextBox>
-
</FooterTemplate>
-
</asp:TemplateField>
-
<asp:TemplateField HeaderText="Address">
-
<ItemTemplate>
-
<asp:Label ID="lblAddress" runat="server" Text='<%#Eval("Address")%>'></asp:Label>
-
</ItemTemplate>
-
<EditItemTemplate>
-
<asp:TextBox ID="gvAddress" runat="server" Text='<%#Bind("Address")%>'></asp:TextBox>
-
</EditItemTemplate>
-
<FooterTemplate>
-
<asp:TextBox ID="gvAddress" runat="server"></asp:TextBox>
-
</FooterTemplate>
-
</asp:TemplateField>
-
<asp:TemplateField HeaderText="MobileNo">
-
<ItemTemplate>
-
<asp:Label ID="lblMobileNo" runat="server" Text='<%#Eval("MobileNo")%>'></asp:Label>
-
</ItemTemplate>
-
<EditItemTemplate>
-
<asp:TextBox ID="gvMobileNo" runat="server" Text='<%#Bind("MobileNo")%>'></asp:TextBox>
-
</EditItemTemplate>
-
<FooterTemplate>
-
<asp:TextBox ID="gvMobileNo" runat="server"></asp:TextBox>
-
</FooterTemplate>
-
</asp:TemplateField>
-
<asp:TemplateField HeaderText="Image">
-
<ItemTemplate>
-
<asp:Label ID="lblImage" runat="server" Text='<%#Eval("Image")%>'></asp:Label>
-
</ItemTemplate>
-
<EditItemTemplate>
-
<asp:TextBox ID="gvImage" runat="server" Text='<%#Bind("Image")%>'></asp:TextBox>
-
</EditItemTemplate>
-
<FooterTemplate>
-
<asp:TextBox ID="gvImage" runat="server" Text='<%#Bind("Image")%>'></asp:TextBox>
-
</FooterTemplate>
-
</asp:TemplateField>
-
<asp:TemplateField HeaderText="Operations">
-
<ItemTemplate>
-
<asp:LinkButton ID="editRow" runat="server" CommandName="Edit" CommandArgument='<%# Container.DataItemIndex %>' Text="Edit" ForeColor="#284775"></asp:LinkButton>
-
<asp:LinkButton ID="deleteRow" runat="server" CommandName="Delete" CommandArgument='<%# Container.DataItemIndex %>' Text="Delete" ForeColor="#284775"></asp:LinkButton>
-
<asp:LinkButton ID="insertRow" runat="server" CommandName="Insert" CommandArgument='<%# Container.DataItemIndex %>' Text="Insert" ForeColor="#284775"></asp:LinkButton>
-
</ItemTemplate>
-
<EditItemTemplate>
-
<asp:LinkButton ID="btnupdate" runat="server" CommandName="Update" CommandArgument='<%# Container.DataItemIndex %>' Text="Update"
-
ForeColor="#284775"></asp:LinkButton>
-
<asp:LinkButton ID="btncancel" runat="server" CommandName="Cancel" CommandArgument='<%# Container.DataItemIndex %>' Text="Cancel"
-
ForeColor="#284775"></asp:LinkButton>
-
</EditItemTemplate>
-
<FooterTemplate>
-
<asp:LinkButton ID="btnAdd" runat="server" CommandName="InsertNew" Text="Insert New"
-
ForeColor="White"></asp:LinkButton>
-
</FooterTemplate>
-
</asp:TemplateField>
-
</Columns>
-
<RowStyle BackColor="#F7F6F3" ForeColor="#000000" />
-
<HeaderStyle BackColor="#284775" ForeColor="AliceBlue" Font-Bold="true" />
-
<FooterStyle BackColor="#284775" ForeColor="AliceBlue" />
-
<PagerStyle BackColor="#A2B5CD" ForeColor="White" HorizontalAlign="Center" />
-
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
-
<EditRowStyle BackColor="#999999" />
-
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
-
</asp:GridView>
Here is the DummyBLL class that I'm using as the DataSource for the GridView. It's pretty much the same except that it has an Insert method that allows me to insert a new DummyBLL. I also changed the default list of DummyBLLs that is used for the source so that the DummyBLL's ID's start at 1 instead of 0 (like in the previous version).
The reason I started the IDs at 1 was to be able to tell if the user was editing a "new" DummyBLL by checking if the ID is 0. If the DummyBLL's ID is 0 then, when the user is canceling the edit I remove it from the data source...likewise if the user is updating I will assign the DummyBLL the real ID at that point.
Here is the code for the DummyBLL class.
Notice how I added the Insert method. I also modified the Update method. In the Update method I check to see if it's a "new" DummyBLL (id = 0) and if so I assign it a new ID. I also changed the Update method to not just add a new DummyBLL onto the end of my list...it retrieves the index of the Dummy being edited and updates the Dummy at that index.
-
protected void gv_Deleting(object sender, GridViewDeleteEventArgs e)
-
{
-
Label lbl = (Label)gv_DisplayClient.Rows[e.RowIndex].FindControl("DummyID");
-
-
Int32 dummyID = Convert.ToInt32(lbl.Text);
-
DummyBLL.delete(dummyID);
-
display();
-
}
-
-
private void display()
-
{
-
gv_DisplayClient.DataSource = DummyBLL.display();
-
gv_DisplayClient.DataBind();
-
}
-
-
private class DummyBLL
-
{
-
private static List<DummyBLL> _source;
-
private Int32 _id;
-
private String _name;
-
private String _address;
-
private String _mobileNo;
-
private String _image;
-
-
public Int32 DummyID
-
{
-
get { return _id; }
-
set { _id = value; }
-
}
-
public String ClientName
-
{
-
get { return _name; }
-
set { _name = value; }
-
}
-
public String Address
-
{
-
get { return _address; }
-
set { _address = value; }
-
}
-
public String MobileNo
-
{
-
get { return _mobileNo; }
-
set { _mobileNo = value; }
-
}
-
public String Image
-
{
-
get { return _image; }
-
set { _image = value; }
-
}
-
-
public static List<DummyBLL> display()
-
{
-
List<DummyBLL> source = new List<DummyBLL>();
-
if (HttpContext.Current.Session["_source"] != null)
-
{
-
_source = (List<DummyBLL>)HttpContext.Current.Session["_source"];
-
source = _source;
-
}
-
else
-
{
-
String[] names = { "Bob", "Sally", "Frank", "Steve", "Beth", "Pat", "Joan", "Howard" };
-
-
int i = 0;
-
foreach (string name in names)
-
{
-
source.Add(new DummyBLL(i + 1, name, name + "'s address", "1-999-333-222" + i.ToString(), name + "'s picture"));
-
i++;
-
}
-
}
-
_source = source;
-
HttpContext.Current.Session["_source"] = _source;
-
return source;
-
}
-
public DummyBLL()
-
{
-
_id = 0;
-
_name = "";
-
_address = "";
-
_mobileNo = "";
-
_image = "";
-
}
-
public DummyBLL(Int32 id, String name, String address, String mobileNo, String imgName)
-
{
-
_id = id;
-
_name = name;
-
_address = address;
-
_mobileNo = mobileNo;
-
_image = imgName;
-
}
-
public static void add(DummyBLL newDummyBLL)
-
{
-
_source = display();
-
-
if (newDummyBLL.DummyID == 0)
-
{
-
newDummyBLL.DummyID = _source.Count + 1;
-
}
-
-
_source.Add(newDummyBLL);
-
HttpContext.Current.Session["_source"] = _source;
-
}
-
public static void delete(Int32 id)
-
{
-
_source = display();
-
DummyBLL toDelete = Array.Find<DummyBLL>(_source.ToArray(), (x) => x.DummyID == id);
-
if (toDelete != null)
-
{
-
_source.Remove(toDelete);
-
}
-
HttpContext.Current.Session["_source"] = _source;
-
}
-
public static void update(DummyBLL updatedDummy)
-
{
-
Int32 id = updatedDummy.DummyID;
-
_source = display();
-
DummyBLL toDelete = Array.Find<DummyBLL>(_source.ToArray(), (x) => x.DummyID == id);
-
-
if (toDelete != null)
-
{
-
Int32 indexOfDummyDeleted = Array.FindIndex(_source.ToArray(), (x) => x.DummyID == toDelete.DummyID);
-
_source.Remove(toDelete);
-
if (id == 0)
-
{
-
updatedDummy.DummyID = _source.Count + 1;
-
}
-
_source.Insert(indexOfDummyDeleted, updatedDummy);
-
}
-
else
-
{
-
if (id == 0)
-
{
-
updatedDummy.DummyID = _source.Count + 1;
-
}
-
_source.Add(updatedDummy);
-
}
-
HttpContext.Current.Session["_source"] = _source;
-
}
-
public static void Insert(DummyBLL newDummy, Int32 index)
-
{
-
_source = display();
-
_source.Insert(index, newDummy);
-
HttpContext.Current.Session["_source"] = _source;
-
}
-
-
}
Last but not least this is the code for the RowCommand, Paging, and the other GridView events...as well as the Page Load event. The RowCommand does everything that I outlined in the steps that you need to do in order to insert a new row.
-
protected void Page_Load(object sender, EventArgs e)
-
{ if (IsPostBack == false)
-
{ display(); }
-
}
-
-
protected void gv_DisplayClient_RowCommand(Object sender, GridViewCommandEventArgs e)
-
{
-
switch (e.CommandName)
-
{
-
case "Insert":
-
-
//The Command argument is the index of the row in the GridView clicked on.
-
//If the GridView supports paging, this is not necessarily the index of the row being shown.
-
//To get the index of the row you need to take into consideration the page size etc.
-
int insertRowIndex;
-
if (int.TryParse(e.CommandArgument.ToString(), out insertRowIndex))
-
{
-
insertRowIndex = insertRowIndex - (gv_DisplayClient.PageIndex * gv_DisplayClient.PageSize);
-
GridViewRow row = gv_DisplayClient.Rows[insertRowIndex];
-
int id = Convert.ToInt32(((Label)row.FindControl("DummyID")).Text);
-
DummyBLL newDummy = new DummyBLL();
-
Int32 insertIndex = Array.FindIndex(DummyBLL.display().ToArray(), (d) => d.DummyID == id);
-
DummyBLL.Insert(newDummy, insertIndex + 1);
-
-
//Checking to see if the index of the row we are editing is on the same page
-
//If it's not then we need to change to the page where the item is inserted
-
if (insertRowIndex + 1 > gv_DisplayClient.PageSize-1)
-
{
-
gv_DisplayClient.PageIndex = gv_DisplayClient.PageIndex + 1;
-
gv_DisplayClient.EditIndex = 0;
-
}
-
else
-
{
-
gv_DisplayClient.EditIndex = insertRowIndex + 1;
-
}
-
display();
-
}
-
break;
-
case "InsertNew":
-
GridViewRow rowNewToInsert = gv_DisplayClient.FooterRow;
-
if (rowNewToInsert != null)
-
{
-
gv_DisplayClient.EditIndex = -1;
-
-
TextBox tx1 = (TextBox)rowNewToInsert.FindControl("gvClientName");
-
TextBox tx2 = (TextBox)rowNewToInsert.FindControl("gvAddress");
-
TextBox tx3 = (TextBox)rowNewToInsert.FindControl("gvMobileNo");
-
TextBox tx4 = (TextBox)rowNewToInsert.FindControl("gvImage");
-
DummyBLL dummy = new DummyBLL();
-
dummy.ClientName = tx1.Text;
-
dummy.Address = tx2.Text;
-
dummy.MobileNo = tx3.Text;
-
dummy.Image = tx4.Text;
-
DummyBLL.add(dummy);
-
display();
-
}
-
break;
-
case "Cancel":
-
int cancelRowIndex;
-
if (int.TryParse(e.CommandArgument.ToString(), out cancelRowIndex))
-
{
-
cancelRowIndex = cancelRowIndex - (gv_DisplayClient.PageIndex * gv_DisplayClient.PageSize);
-
GridViewRow row = gv_DisplayClient.Rows[cancelRowIndex];
-
int id = Convert.ToInt32(((Label)row.FindControl("DummyID")).Text);
-
if (id == 0)
-
{
-
DummyBLL.delete(id);
-
}
-
}
-
-
break;
-
}
-
}
-
-
protected void gv_DisplayClient_OnPageIndexChanging(object sender, GridViewPageEventArgs e)
-
{
-
if (e.NewPageIndex >= 0 && e.NewPageIndex < gv_DisplayClient.PageCount)
-
{
-
gv_DisplayClient.PageIndex = e.NewPageIndex;
-
display();
-
}
-
}
-
protected void GvEditing(object sender, GridViewEditEventArgs e)
-
{
-
gv_DisplayClient.EditIndex = e.NewEditIndex;
-
display();
-
}
-
protected void gv_EditCancel(object sender, GridViewCancelEditEventArgs e)
-
{
-
gv_DisplayClient.EditIndex = -1;
-
display();
-
}
-
protected void gv_Updating(object sender, GridViewUpdateEventArgs e)
-
{
-
-
Label lb = (Label)gv_DisplayClient.Rows[e.RowIndex].FindControl("DummyID");
-
int ClientId = Convert.ToInt32(lb.Text);
-
TextBox tx1 = (TextBox)gv_DisplayClient.Rows[e.RowIndex].FindControl("gvClientName");
-
TextBox tx2 = (TextBox)gv_DisplayClient.Rows[e.RowIndex].FindControl("gvAddress");
-
TextBox tx3 = (TextBox)gv_DisplayClient.Rows[e.RowIndex].FindControl("gvMobileNo");
-
TextBox tx4 = (TextBox)gv_DisplayClient.Rows[e.RowIndex].FindControl("gvImage");
-
DummyBLL dummy = new DummyBLL();
-
dummy.ClientName = tx1.Text;
-
dummy.Address = tx2.Text;
-
dummy.MobileNo = tx3.Text;
-
dummy.Image = tx4.Text;
-
dummy.DummyID = ClientId;
-
DummyBLL.update(dummy);
-
gv_DisplayClient.EditIndex = -1;
-
display();
-
-
}
-
protected void gv_Deleting(object sender, GridViewDeleteEventArgs e)
-
{
-
Label lbl = (Label)gv_DisplayClient.Rows[e.RowIndex].FindControl("DummyID");
-
-
Int32 dummyID = Convert.ToInt32(lbl.Text);
-
DummyBLL.delete(dummyID);
-
display();
-
}
-
-
private void display()
-
{
-
gv_DisplayClient.DataSource = DummyBLL.display();
-
gv_DisplayClient.DataBind();
-
}
-
-Frinny