I need something in asp to allow me to add, edit and remove data like you get in the DataGridView.
Is there something similar in asp? I only found the GridView and it really sucks compared to the DataGridView.
Please, any help will be appreciated.
Printable View
I need something in asp to allow me to add, edit and remove data like you get in the DataGridView.
Is there something similar in asp? I only found the GridView and it really sucks compared to the DataGridView.
Please, any help will be appreciated.
Hello,
In what way does it "suck" exactly?
The GridView is one of the most versatile and usable controls within ASP.Net, and I am struggling to understand why you are having a problem.
Gary
It might be my lack of knowledge about the tool.
I'm use to using the datagridview in vb.net where i can populate it from my database and still have a clean line at the bottom of the dgv rows where i can add a new line/record by just filling in the cells in the dgv.
I just cant for the life of me figure out how to do that in asp.net.
Well THIS-IS-ASP.NET! (ehm bad imitation of THIS-IS-SPARTA!) so not every control will work the same as a winforms app.This doesn't mean that it's not a good control (i kinda hated the gridview when i first ever laid eyes on it in good ol' 2005 but that is another story)
I didn't come to a situation i should use that but it can be done.
The solution i like most would be to use the footertemplate (i prefer templates for handling the gridview you have much more flexibility with data manipulation).
Here some examples:
http://www.4guysfromrolla.com/articles/021203-1.aspx
http://www.revenmerchantservices.com...d-new-row.aspx
and a general discussion example:
http://forums.asp.net/t/1217361.aspx
Hello,
I have to agree with sap (this is becoming a bit of a habit :))
At the end of the day, there are things that controls in a Windows Forms Application will be able to do out of the box, that controls in a Web Forms Applications can't, and vice versa. Normally this is by design, and down to the differences in the way these applications work.
In this example for instance you could combine a GridView with a FormView or a DetailsView to provide the ability to insert items into the GridView. Or, as sap has pointed out, add another row to the GridView to provide the additional functionality.
Gary
Haha thanks guys.
Yes, I noticed there are a lot of "good looking' tools available in asp.net. I just don't know how to use these tools. Thanks for the links. I will work through them and hopefully complete my task before the end of the day.
Let us know how you get on, and whether you have any other questions.
Gary
Thanks a lot for the great help. I got it working. I'll post my code if someone would like to see it.
The HTML:
HTML Code:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="DataGrid._Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>Untitled Page</title> <style type="text/css"> .style1 { width: 173px; } .style2 { width: 416px; } </style> </head> <body> <form id="form1" runat="server"> <div style="width:550px;"> <table style="width:100%;"> <tr> <td colspan="2"> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" ShowFooter="True" Width="100%"> <Columns> <asp:TemplateField HeaderText="ID"> <ItemTemplate> <asp:Label ID="GVlblID" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"ID") %>'></asp:Label> </ItemTemplate> <FooterTemplate> <asp:TextBox ID="GVtxtID" runat="server"></asp:TextBox> </FooterTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="First Name"> <ItemTemplate> <asp:Label ID="GVlblfirstName" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"FirstName") %>'></asp:Label> </ItemTemplate> <FooterTemplate> <asp:TextBox ID="GVtxtFirstName" runat="server"></asp:TextBox> </FooterTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Last Name"> <ItemTemplate> <asp:Label ID="GVlblLastName" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"LastName") %>'></asp:Label> </ItemTemplate> <FooterTemplate> <asp:TextBox ID="GVtxtLastName" runat="server"></asp:TextBox> </FooterTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Email"> <ItemTemplate> <asp:Label ID="GVlblEmail" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"Email") %>'></asp:Label> </ItemTemplate> <FooterTemplate> <asp:TextBox ID="GVtxtEmail" runat="server"></asp:TextBox> </FooterTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Check all"> <HeaderTemplate> <asp:CheckBox ID="GVCB2" AutoPostBack="true" runat="server" OnCheckedChanged="GVCB2_CheckedChanged1" /> </HeaderTemplate> <ItemTemplate> <asp:CheckBox ID="GVCB1" runat="server" /> </ItemTemplate> <FooterTemplate> <asp:Button ID="GVbtnSAve" runat="server" Text="Save" OnClick="GVbtnSAve_Click" /> </FooterTemplate> </asp:TemplateField> </Columns> </asp:GridView> </td> </tr> <tr> <td class="style2"> <asp:Label ID="GVErrorLabel" runat="server" Font-Size="XX-Small" ForeColor="Red"></asp:Label> </td> <td class="style1" style="text-align:right;"> <asp:Button ID="btnDelete" runat="server" style="height: 26px" onclick="btnDelete_Click" Text="Delete" /> </td> </tr> </table> </div> </form> </body> </html>
The Backend in C#:
C# Code:
using System; using System.Collections; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; namespace DataGrid { public partial class _Default : System.Web.UI.Page { protected DataTable GVdt = new DataTable(); int GVRowCounter = 0; protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { Do_Load(); } else { GVdt = (DataTable)(Session["FollowUpNotesDT"]); GVRowCounter = (int)(Session["FollowUpNotesRowCounter"]); } } private void Do_Load() { GVErrorLabel.Text = ""; try { GVdt.Columns.Add("ID"); GVdt.Columns.Add("FirstName"); GVdt.Columns.Add("LastName"); GVdt.Columns.Add("Email"); //i'm just hardcoding in some data at the moment. This will be looped from database. GVdt.Rows.Add("1", "Jack", "Johnson", "[email protected]"); GVdt.Rows.Add("2", "Max", "Drewer", "[email protected]"); GVdt.Rows.Add("3", "Steve", "Hetzner", "[email protected]"); GVRowCounter = 5; // this will increment in loop when you populate datagrid Session["FollowUpNotesRowCounter"] = GVRowCounter; Session["FollowUpNotesDT"] = GVdt; BindGrid(); } catch (Exception e) { string GVError = ""; GVError = Convert.ToString(e.Message); GVErrorLabel.Text = "Unable to load data: " + GVError; } } private void BindGrid() { GVErrorLabel.Text = ""; try { Do_BLANK(); GridView1.DataSource = GVdt; GridView1.DataBind(); GridView1.Columns[0].Visible = false; } catch (Exception e) { string GVError = ""; GVError = Convert.ToString(e.Message); GVErrorLabel.Text = "Unable to load data: " + GVError; } } protected void Do_BLANK() { if (GVdt.Rows.Count < 1) { GVdt.Rows.Add("<<BLANK>>","","",""); } else { try { foreach (GridViewRow r in GridView1.Rows) { Boolean DeletedSomething = false; //LOOPING THE GV TO FIND A ROW THAT IS CHECKED FOR DELETE DeletedSomething = true; try { //GET THE STRING ID string GVTheID = "<<BLANK>>"; //GridView1.DeleteRow(GVID); for (int i = 0; i < GVdt.Rows.Count; ) { string tmpID = GVdt.Rows[i][0].ToString(); if (GVTheID == tmpID) { GVdt.Rows.RemoveAt(i); i = 0; } i++; } } catch { } } } catch { } } } protected void GV_DELETE() { GVErrorLabel.Text = ""; try { foreach (GridViewRow r in GridView1.Rows) { Boolean DeletedSomething = false; //LOOPING THE GV TO FIND A ROW THAT IS CHECKED FOR DELETE Boolean DelChecked = ((CheckBox)r.FindControl("GVCB1")).Checked; if (DelChecked == true) { DeletedSomething = true; try { //GET THE STRING ID string GVTheID = ((Label)r.FindControl("GVlblID")).Text; //GridView1.DeleteRow(GVID); for (int i = 0; i < GVdt.Rows.Count; ) { string tmpID = GVdt.Rows[i][0].ToString(); if (GVTheID == tmpID) { GVdt.Rows.RemoveAt(i); i = 0; } i++; } } catch { } } //IF I HAVE DELETED SOMETHING, I WANT TO MY DATATABLE if (DeletedSomething == true) { Session["FollowUpNotesDT"] = GVdt; BindGrid(); } } } catch (Exception e) { string GVError = ""; GVError = Convert.ToString(e.Message); GVErrorLabel.Text = "Unable to load data: " + GVError; } } protected void Add_Line() { GVErrorLabel.Text = ""; try { int rw = 0; rw = GVRowCounter; rw++; GVRowCounter = rw; Session["FollowUpNotesRowCounter"] = GVRowCounter; string GVstrID = Convert.ToString(rw); string GVstrFirstName = ((TextBox)GridView1.FooterRow.FindControl("GVtxtFirstName")).Text; string GVstrLastName = ((TextBox)GridView1.FooterRow.FindControl("GVtxtLastName")).Text; string GVstrEmail = ((TextBox)GridView1.FooterRow.FindControl("GVtxtEmail")).Text; if (GVstrFirstName.Length < 1) { GVErrorLabel.Text = "Invalid FirstName!"; // * * * * POP A JAVASCRIPT MESSAGE //string javaScript = "<script language=JavaScript>\n" + "alert('The first name field is blank.');\n" + "</script>"; // RegisterStartupScript("WarningScript", javaScript); return; } GVdt.Rows.Add(GVstrID, GVstrFirstName, GVstrLastName, GVstrEmail); BindGrid(); } catch (Exception e) { string GVError = ""; GVError = Convert.ToString(e.Message); GVErrorLabel.Text = "Unable to load data: " + GVError; } } protected void GVCB2_CheckedChanged1(object sender, EventArgs e) { foreach (GridViewRow r in GridView1.Rows) { ((CheckBox)r.FindControl("GVCB1")).Checked = true; } } protected void GVbtnSAve_Click(object sender, EventArgs e) { Add_Line(); } protected void btnDelete_Click(object sender, EventArgs e) { GV_DELETE(); } } }
The rep, i meant the reputation, my bad.