Results 1 to 13 of 13

Thread: [RESOLVED] Help me with project

  1. #1

    Thread Starter
    Addicted Member tgf-47's Avatar
    Join Date
    Feb 2010
    Location
    CapeTown, South Africa -34.01244,18.337415
    Posts
    209

    Resolved [RESOLVED] Help me with project

    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.

  2. #2
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Help me with project

    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

  3. #3

    Thread Starter
    Addicted Member tgf-47's Avatar
    Join Date
    Feb 2010
    Location
    CapeTown, South Africa -34.01244,18.337415
    Posts
    209

    Re: Help me with project

    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.

  4. #4
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,605

    Re: Help me with project

    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
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  5. #5
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Help me with project

    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

  6. #6

    Thread Starter
    Addicted Member tgf-47's Avatar
    Join Date
    Feb 2010
    Location
    CapeTown, South Africa -34.01244,18.337415
    Posts
    209

    Re: Help me with project

    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.

  7. #7
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Help me with project

    Let us know how you get on, and whether you have any other questions.

    Gary

  8. #8

    Thread Starter
    Addicted Member tgf-47's Avatar
    Join Date
    Feb 2010
    Location
    CapeTown, South Africa -34.01244,18.337415
    Posts
    209

    Talking Re: Help me with project

    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:
    1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="DataGrid._Default" %>
    2.  
    3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    4.  
    5. <html xmlns="http://www.w3.org/1999/xhtml" >
    6. <head runat="server">
    7.     <title>Untitled Page</title>
    8.     <style type="text/css">
    9.         .style1
    10.         {
    11.             width: 173px;
    12.         }
    13.         .style2
    14.         {
    15.             width: 416px;
    16.         }
    17.     </style>
    18. </head>
    19. <body>
    20.     <form id="form1" runat="server">
    21.     <div style="width:550px;">          
    22.           <table style="width:100%;">
    23.               <tr>
    24.                   <td colspan="2">
    25.                         <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" ShowFooter="True" Width="100%">
    26.                             <Columns>
    27.                                 <asp:TemplateField HeaderText="ID">
    28.                                     <ItemTemplate>
    29.                                         <asp:Label ID="GVlblID" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"ID") %>'></asp:Label>
    30.                                     </ItemTemplate>
    31.                                     <FooterTemplate>
    32.                                         <asp:TextBox ID="GVtxtID" runat="server"></asp:TextBox>
    33.                                     </FooterTemplate>
    34.                                  
    35.                                 </asp:TemplateField>
    36.                                 <asp:TemplateField HeaderText="First Name">
    37.                                     <ItemTemplate>
    38.                                         <asp:Label ID="GVlblfirstName" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"FirstName") %>'></asp:Label>
    39.                                     </ItemTemplate>
    40.                                     <FooterTemplate>
    41.                                         <asp:TextBox ID="GVtxtFirstName" runat="server"></asp:TextBox>
    42.                                     </FooterTemplate>
    43.                                 </asp:TemplateField>
    44.                                 <asp:TemplateField HeaderText="Last Name">
    45.                                     <ItemTemplate>
    46.                                         <asp:Label ID="GVlblLastName" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"LastName") %>'></asp:Label>
    47.                                     </ItemTemplate>
    48.                                     <FooterTemplate>
    49.                                         <asp:TextBox ID="GVtxtLastName" runat="server"></asp:TextBox>
    50.                                     </FooterTemplate>
    51.                                 </asp:TemplateField>
    52.                                 <asp:TemplateField HeaderText="Email">
    53.                                     <ItemTemplate>
    54.                                         <asp:Label ID="GVlblEmail" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"Email") %>'></asp:Label>
    55.                                     </ItemTemplate>
    56.                                     <FooterTemplate>
    57.                                         <asp:TextBox ID="GVtxtEmail" runat="server"></asp:TextBox>
    58.                                     </FooterTemplate>
    59.                                 </asp:TemplateField>
    60.                                 <asp:TemplateField HeaderText="Check all">
    61.                                     <HeaderTemplate>
    62.                                         <asp:CheckBox ID="GVCB2" AutoPostBack="true" runat="server"  OnCheckedChanged="GVCB2_CheckedChanged1" />
    63.                                     </HeaderTemplate>
    64.                                     <ItemTemplate>
    65.                                         <asp:CheckBox ID="GVCB1"  runat="server" />
    66.                                     </ItemTemplate>
    67.                                     <FooterTemplate>
    68.                                         <asp:Button ID="GVbtnSAve" runat="server" Text="Save" OnClick="GVbtnSAve_Click" />              
    69.                                     </FooterTemplate>
    70.                                 </asp:TemplateField>
    71.                             </Columns>
    72.                         </asp:GridView>
    73.                   </td>                  
    74.               </tr>
    75.               <tr>
    76.                   <td class="style2">
    77.                       <asp:Label ID="GVErrorLabel" runat="server" Font-Size="XX-Small"
    78.                           ForeColor="Red"></asp:Label>
    79.                   </td>
    80.                   <td class="style1" style="text-align:right;">
    81.                       <asp:Button ID="btnDelete" runat="server" style="height: 26px" onclick="btnDelete_Click" Text="Delete" />
    82.                   </td>                
    83.               </tr>              
    84.           </table>
    85.        
    86.     </div>
    87.     </form>
    88. </body>
    89. </html>

  9. #9

    Thread Starter
    Addicted Member tgf-47's Avatar
    Join Date
    Feb 2010
    Location
    CapeTown, South Africa -34.01244,18.337415
    Posts
    209

    Re: Help me with project

    The Backend in C#:
    C# Code:
    1. using System;
    2. using System.Collections;
    3. using System.Configuration;
    4. using System.Data;
    5. using System.Linq;
    6. using System.Web;
    7. using System.Web.Security;
    8. using System.Web.UI;
    9. using System.Web.UI.HtmlControls;
    10. using System.Web.UI.WebControls;
    11. using System.Web.UI.WebControls.WebParts;
    12. using System.Xml.Linq;
    13.  
    14. namespace DataGrid
    15. {
    16.     public partial class _Default : System.Web.UI.Page
    17.     {
    18.         protected DataTable GVdt = new DataTable();
    19.         int GVRowCounter = 0;
    20.         protected void Page_Load(object sender, EventArgs e)
    21.         {
    22.             if (!Page.IsPostBack)
    23.             {
    24.                 Do_Load();                
    25.             }
    26.             else
    27.             {
    28.                 GVdt = (DataTable)(Session["FollowUpNotesDT"]);
    29.                 GVRowCounter = (int)(Session["FollowUpNotesRowCounter"]);
    30.             }
    31.         }
    32.  
    33.         private void Do_Load()
    34.         {
    35.             GVErrorLabel.Text = "";
    36.             try
    37.             {
    38.                 GVdt.Columns.Add("ID");
    39.                 GVdt.Columns.Add("FirstName");
    40.                 GVdt.Columns.Add("LastName");
    41.                 GVdt.Columns.Add("Email");
    42.                 //i'm just hardcoding in some data at the moment. This will be looped from database.
    43.                 GVdt.Rows.Add("1", "Jack", "Johnson", "jt@gmail.com");
    44.                 GVdt.Rows.Add("2", "Max", "Drewer", "FST@gmail.com");
    45.                 GVdt.Rows.Add("3", "Steve", "Hetzner", "SH@gmail.com");
    46.                 GVRowCounter = 5; // this will increment in loop when you populate datagrid
    47.                 Session["FollowUpNotesRowCounter"] = GVRowCounter;
    48.                 Session["FollowUpNotesDT"] = GVdt;
    49.                 BindGrid();
    50.             }
    51.             catch (Exception e)
    52.             {
    53.                 string GVError = ""; GVError = Convert.ToString(e.Message);
    54.                 GVErrorLabel.Text = "Unable to load data: " + GVError;
    55.             }
    56.         }
    57.  
    58.         private void BindGrid()
    59.         {
    60.             GVErrorLabel.Text = "";
    61.             try
    62.             {
    63.                 Do_BLANK();
    64.                 GridView1.DataSource = GVdt;
    65.                 GridView1.DataBind();
    66.                 GridView1.Columns[0].Visible = false;
    67.             }
    68.             catch (Exception e)
    69.             {
    70.                 string GVError = ""; GVError = Convert.ToString(e.Message);
    71.                 GVErrorLabel.Text = "Unable to load data: " + GVError;
    72.             }
    73.         }
    74.  
    75.         protected void Do_BLANK()
    76.         {
    77.             if (GVdt.Rows.Count < 1)
    78.             {
    79.                 GVdt.Rows.Add("<<BLANK>>","","","");
    80.             }
    81.             else
    82.             {
    83.                 try
    84.                 {
    85.                     foreach (GridViewRow r in GridView1.Rows)
    86.                     {
    87.                         Boolean DeletedSomething = false;
    88.                         //LOOPING THE GV TO FIND A ROW THAT IS CHECKED FOR DELETE                                            
    89.                         DeletedSomething = true;
    90.                         try
    91.                         {
    92.                             //GET THE STRING ID
    93.                             string GVTheID = "<<BLANK>>";
    94.                             //GridView1.DeleteRow(GVID);
    95.  
    96.                             for (int i = 0; i < GVdt.Rows.Count; )
    97.                             {
    98.                                 string tmpID = GVdt.Rows[i][0].ToString();
    99.                                 if (GVTheID == tmpID)
    100.                                 {
    101.                                     GVdt.Rows.RemoveAt(i);
    102.                                     i = 0;
    103.                                 }
    104.                                 i++;
    105.                             }
    106.                         }
    107.                         catch
    108.                         {
    109.                         }
    110.  
    111.                     }
    112.                 }
    113.                 catch
    114.                 {
    115.                 }
    116.             }
    117.         }  
    118.  
    119.         protected void GV_DELETE()
    120.         {
    121.            GVErrorLabel.Text = "";
    122.            try
    123.            {
    124.                foreach (GridViewRow r in GridView1.Rows)
    125.                {
    126.                    Boolean DeletedSomething = false;
    127.                    //LOOPING THE GV TO FIND A ROW THAT IS CHECKED FOR DELETE
    128.                    Boolean DelChecked = ((CheckBox)r.FindControl("GVCB1")).Checked;
    129.                    if (DelChecked == true)
    130.                    {
    131.                        DeletedSomething = true;
    132.                        try
    133.                        {
    134.                            //GET THE STRING ID
    135.                            string GVTheID = ((Label)r.FindControl("GVlblID")).Text;                          
    136.                            //GridView1.DeleteRow(GVID);
    137.  
    138.                            for (int i = 0; i < GVdt.Rows.Count; )
    139.                            {
    140.                                string tmpID = GVdt.Rows[i][0].ToString();
    141.                                if (GVTheID == tmpID)
    142.                                {
    143.                                    GVdt.Rows.RemoveAt(i);
    144.                                    i = 0;
    145.                                }
    146.                                i++;
    147.                            }
    148.                        }
    149.                        catch { }
    150.                    }
    151.  
    152.                    //IF I HAVE DELETED SOMETHING, I WANT TO  MY DATATABLE
    153.                    if (DeletedSomething == true)
    154.                    {
    155.                        Session["FollowUpNotesDT"] = GVdt;
    156.                        BindGrid();
    157.                    }
    158.  
    159.  
    160.                }
    161.            }
    162.            catch (Exception e)
    163.            {
    164.                string GVError = ""; GVError = Convert.ToString(e.Message);
    165.                GVErrorLabel.Text = "Unable to load data: " + GVError;
    166.            }
    167.         }
    168.  
    169.         protected void Add_Line()
    170.         {
    171.             GVErrorLabel.Text = "";
    172.             try
    173.             {                
    174.                 int rw = 0; rw = GVRowCounter;
    175.                 rw++;
    176.                 GVRowCounter = rw;
    177.                 Session["FollowUpNotesRowCounter"] = GVRowCounter;
    178.                 string GVstrID = Convert.ToString(rw);
    179.                 string GVstrFirstName = ((TextBox)GridView1.FooterRow.FindControl("GVtxtFirstName")).Text;
    180.                 string GVstrLastName = ((TextBox)GridView1.FooterRow.FindControl("GVtxtLastName")).Text;
    181.                 string GVstrEmail = ((TextBox)GridView1.FooterRow.FindControl("GVtxtEmail")).Text;
    182.                 if (GVstrFirstName.Length < 1)
    183.                 {
    184.                     GVErrorLabel.Text = "Invalid FirstName!";
    185.                     // * * * *  POP A JAVASCRIPT MESSAGE
    186.                     //string javaScript =  "<script language=JavaScript>\n" +  "alert('The first name field is blank.');\n" +  "</script>";
    187.                     // RegisterStartupScript("WarningScript", javaScript);
    188.                     return;
    189.                 }
    190.                 GVdt.Rows.Add(GVstrID, GVstrFirstName, GVstrLastName, GVstrEmail);
    191.                 BindGrid();
    192.             }
    193.             catch (Exception e)
    194.             {
    195.                 string GVError = ""; GVError = Convert.ToString(e.Message);
    196.                 GVErrorLabel.Text = "Unable to load data: " + GVError;
    197.             }
    198.         }
    199.         protected void GVCB2_CheckedChanged1(object sender, EventArgs e)
    200.         {
    201.             foreach (GridViewRow r in GridView1.Rows)
    202.             {
    203.                 ((CheckBox)r.FindControl("GVCB1")).Checked = true;
    204.             }
    205.  
    206.         }    
    207.  
    208.         protected void GVbtnSAve_Click(object sender, EventArgs e)
    209.         {
    210.             Add_Line();
    211.         }
    212.  
    213.         protected void btnDelete_Click(object sender, EventArgs e)
    214.         {
    215.             GV_DELETE();
    216.         }
    217.  
    218.     }
    219. }

  10. #10
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,605

    Re: Help me with project

    Quote Originally Posted by gep13 View Post
    Hello,

    I have to agree with sap (this is becoming a bit of a habit )

    Gary
    It's either that i'm starting to learn asp or you are forgetting asp

    Thanks for the reg tgf
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  11. #11
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Help me with project

    Quote Originally Posted by sapator View Post
    Thanks for the reg tgf
    I am confused?!?

  12. #12
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,605

    Re: [RESOLVED] Help me with project

    The rep, i meant the reputation, my bad.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  13. #13
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [RESOLVED] Help me with project

    Quote Originally Posted by sapator View Post
    The rep, i meant the reputation, my bad.
    Ha ha, now that makes a bit more sense

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width