Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Gridview in ASP.Net with C# [modified]

Gridview in ASP.Net with C# [modified]

Scheduled Pinned Locked Moved ASP.NET
csharpasp-netdesigntutorial
11 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mahichandu
    wrote on last edited by
    #1

    Below is my databse table.. I can populate this data into the UI Page.. My task is to automatically delete the columns which has '0'(zero) value...and i have to delete the first row of my table... Hear i am not going to use any itemtemplate column.. I have to write some code in codebehind..

    JurisdictionID JurisdictionDescription Year1 Year2 Year3 Year4 Year5 Year6 Year7
    2005 2006 2009 0 0 0 0
    AL Alabama 0 0 6 0 0 0 0
    AK Alaska 2 0 0 0 0 0 0

    Could Anyone suggest me ..How to proceed further in detail with some code...:confused:

    modified on Monday, June 1, 2009 4:24 PM

    I 1 Reply Last reply
    0
    • M mahichandu

      Below is my databse table.. I can populate this data into the UI Page.. My task is to automatically delete the columns which has '0'(zero) value...and i have to delete the first row of my table... Hear i am not going to use any itemtemplate column.. I have to write some code in codebehind..

      JurisdictionID JurisdictionDescription Year1 Year2 Year3 Year4 Year5 Year6 Year7
      2005 2006 2009 0 0 0 0
      AL Alabama 0 0 6 0 0 0 0
      AK Alaska 2 0 0 0 0 0 0

      Could Anyone suggest me ..How to proceed further in detail with some code...:confused:

      modified on Monday, June 1, 2009 4:24 PM

      I Offline
      I Offline
      Ibrahim Bello
      wrote on last edited by
      #2

      foreach (GridViewRow Row in grd.Rows) { for (int i = 0; i<7; i++) { if (Row[i].ToString().Equals("0")) { grd.Columns.Remove(Row[i]); } } } I have not tested this code. Let me know if it works or not so as to fix the errors. Regards

      M 1 Reply Last reply
      0
      • I Ibrahim Bello

        foreach (GridViewRow Row in grd.Rows) { for (int i = 0; i<7; i++) { if (Row[i].ToString().Equals("0")) { grd.Columns.Remove(Row[i]); } } } I have not tested this code. Let me know if it works or not so as to fix the errors. Regards

        M Offline
        M Offline
        mahichandu
        wrote on last edited by
        #3

        hey..i am getting some errors..when am using that code.. in which method we have to write tht code.. i wrote in...

        public void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)

        i am getting errors like this.. 1) 'System.Data.DataRow.DataRow(System.Data.DataRowBuilder)' is inaccessible due to its protection level 2) Cannot apply indexing with [] to an expression of 'System.Web.UI.WebControls.GridViewRow' actually am poor in c# technologies..could you sen dme solution fot this..:confused:

        I 1 Reply Last reply
        0
        • M mahichandu

          hey..i am getting some errors..when am using that code.. in which method we have to write tht code.. i wrote in...

          public void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)

          i am getting errors like this.. 1) 'System.Data.DataRow.DataRow(System.Data.DataRowBuilder)' is inaccessible due to its protection level 2) Cannot apply indexing with [] to an expression of 'System.Web.UI.WebControls.GridViewRow' actually am poor in c# technologies..could you sen dme solution fot this..:confused:

          I Offline
          I Offline
          Ibrahim Bello
          wrote on last edited by
          #4

          Sorry, I was closing from work when I posted. This should work: foreach (GridViewRow Row in GridView1.Rows) { for(int i=0; i < 7; i++) { if (Row.Cells[i].Text == "0") { GridView1.Columns.Remove(GridView1.Columns[i]); } } } I called up this function from a button which I created. You can place it within the GridView1_RowDeleting if you wish without problems. As per deleting first row, I suggest deleting it from database first (using sql query) and re-binding your gridview.

          M 1 Reply Last reply
          0
          • I Ibrahim Bello

            Sorry, I was closing from work when I posted. This should work: foreach (GridViewRow Row in GridView1.Rows) { for(int i=0; i < 7; i++) { if (Row.Cells[i].Text == "0") { GridView1.Columns.Remove(GridView1.Columns[i]); } } } I called up this function from a button which I created. You can place it within the GridView1_RowDeleting if you wish without problems. As per deleting first row, I suggest deleting it from database first (using sql query) and re-binding your gridview.

            M Offline
            M Offline
            mahichandu
            wrote on last edited by
            #5

            hey..thnx for ur reply.. i am writting the code in the row delete event only... i am not getting any errors..when i execute this code...somehow am unable to delete the columns.... is there any pother way to do..?

            I 1 Reply Last reply
            0
            • M mahichandu

              hey..thnx for ur reply.. i am writting the code in the row delete event only... i am not getting any errors..when i execute this code...somehow am unable to delete the columns.... is there any pother way to do..?

              I Offline
              I Offline
              Ibrahim Bello
              wrote on last edited by
              #6

              If what you want to do is to delete a column from the underlying database table I'm not sure I can help you on that. If what you want h/ever is to simply hide the column from the GridView this should work otherwise you can replace GridView1.Columns.Remove(GridView1.Columns[i]); with GridView1.Column[i].Visible = false; I've tested the previous code and it worked. Why not post more code for us to see where you're getting it wrong?

              M 1 Reply Last reply
              0
              • I Ibrahim Bello

                If what you want to do is to delete a column from the underlying database table I'm not sure I can help you on that. If what you want h/ever is to simply hide the column from the GridView this should work otherwise you can replace GridView1.Columns.Remove(GridView1.Columns[i]); with GridView1.Column[i].Visible = false; I've tested the previous code and it worked. Why not post more code for us to see where you're getting it wrong?

                M Offline
                M Offline
                mahichandu
                wrote on last edited by
                #7

                by using above syntax also..i can't see anychange in the table..i can see all the columns..the 0(zero) value cell columns are not hiding..:( actullt my task is to delete those columns..am unable to do that.. atleast i want to try with hiding those columns.. :(( below i copied my code of populating data into gridview and the rowdelete event..

                protected void Page_Load(object sender, EventArgs e)
                {
                PopulateGrid();

                }
                
                public void PopulateGrid()
                {
                    SqlConnection sqlConn = null;
                    SqlDataAdapter da = null;
                
                    try
                    {
                        string connString = null;
                        SqlCommand cmd1;
                        connString = System.Web.Configuration.WebConfigurationManager.AppSettings\["SqlServerConnectionString"\];
                        //connString = "Data Source = SQD-CON4\\QSRV1; Initial Catalog = NDI; Integrated Security = SSPI;";
                        sqlConn = new SqlConnection(connString);
                        sqlConn.Open();
                        cmd1 = new SqlCommand("\[MED\].\[usp\_SelectAllSDNCounts\]", sqlConn);
                        cmd1.CommandType = CommandType.StoredProcedure;
                        da = new SqlDataAdapter(cmd1);
                        DataSet ds = new DataSet();
                        da.Fill(ds);
                        GridView1.DataSource = ds.Tables\[0\].DefaultView;
                        GridView1.DataBind();
                    }
                    finally
                    {
                        sqlConn.Close();
                        da.Dispose();
                        da = null;
                    }
                
                }
                

                public void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
                {

                       foreach (GridViewRow Row in GridView1.Rows)
                        {
                            for(int i=0; i<11; i++)
                            {
                                if (Row.Cells\[i\].Text == "0")
                                {
                                 GridView1.Columns\[i\].Visible=false;
                                }
                            }
                
                        }
                }
                
                I 1 Reply Last reply
                0
                • M mahichandu

                  by using above syntax also..i can't see anychange in the table..i can see all the columns..the 0(zero) value cell columns are not hiding..:( actullt my task is to delete those columns..am unable to do that.. atleast i want to try with hiding those columns.. :(( below i copied my code of populating data into gridview and the rowdelete event..

                  protected void Page_Load(object sender, EventArgs e)
                  {
                  PopulateGrid();

                  }
                  
                  public void PopulateGrid()
                  {
                      SqlConnection sqlConn = null;
                      SqlDataAdapter da = null;
                  
                      try
                      {
                          string connString = null;
                          SqlCommand cmd1;
                          connString = System.Web.Configuration.WebConfigurationManager.AppSettings\["SqlServerConnectionString"\];
                          //connString = "Data Source = SQD-CON4\\QSRV1; Initial Catalog = NDI; Integrated Security = SSPI;";
                          sqlConn = new SqlConnection(connString);
                          sqlConn.Open();
                          cmd1 = new SqlCommand("\[MED\].\[usp\_SelectAllSDNCounts\]", sqlConn);
                          cmd1.CommandType = CommandType.StoredProcedure;
                          da = new SqlDataAdapter(cmd1);
                          DataSet ds = new DataSet();
                          da.Fill(ds);
                          GridView1.DataSource = ds.Tables\[0\].DefaultView;
                          GridView1.DataBind();
                      }
                      finally
                      {
                          sqlConn.Close();
                          da.Dispose();
                          da = null;
                      }
                  
                  }
                  

                  public void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
                  {

                         foreach (GridViewRow Row in GridView1.Rows)
                          {
                              for(int i=0; i<11; i++)
                              {
                                  if (Row.Cells\[i\].Text == "0")
                                  {
                                   GridView1.Columns\[i\].Visible=false;
                                  }
                              }
                  
                          }
                  }
                  
                  I Offline
                  I Offline
                  Ibrahim Bello
                  wrote on last edited by
                  #8

                  Change: protected void Page_Load(object sender, EventArgs e) { PopulateGrid(); } to: protected void Page_Load(object sender, EventArgs e) { if(!Page.IsPostBack) { PopulateGrid(); } } Does this help? :)

                  M 1 Reply Last reply
                  0
                  • I Ibrahim Bello

                    Change: protected void Page_Load(object sender, EventArgs e) { PopulateGrid(); } to: protected void Page_Load(object sender, EventArgs e) { if(!Page.IsPostBack) { PopulateGrid(); } } Does this help? :)

                    M Offline
                    M Offline
                    mahichandu
                    wrote on last edited by
                    #9

                    no..hmmm:( it remains the same..i can see the table with all column .. if u want to know my design page ..see below..

                    %@ Page Language="C#" AutoEventWireup="true" CodeFile="MedPending.aspx.cs" Inherits="Maintenance_MedPending" %>

                    Untitled Page
                    

                    :confused:

                    I 2 Replies Last reply
                    0
                    • M mahichandu

                      no..hmmm:( it remains the same..i can see the table with all column .. if u want to know my design page ..see below..

                      %@ Page Language="C#" AutoEventWireup="true" CodeFile="MedPending.aspx.cs" Inherits="Maintenance_MedPending" %>

                      Untitled Page
                      

                      :confused:

                      I Offline
                      I Offline
                      Ibrahim Bello
                      wrote on last edited by
                      #10

                      1. Try putting a breakpoint in the code provided and see if the Row's Cell Text ever evaluates to "0". Does it ever enter the function i gave you? 2. Have you hooked the Row_Deleting method to the control calling it? If not add: OnRowDeleting = "GridView1_RowDeleting" to this: OnRowDeleting = "GridView1_RowDeleting" CssClass="GridRowGrayBorder"> Or alternatively in Page_Load add: GridView1.RowDeleting+=new GridViewDeleteEventHandler(GridView1_RowDeleting); Hope this helps

                      1 Reply Last reply
                      0
                      • M mahichandu

                        no..hmmm:( it remains the same..i can see the table with all column .. if u want to know my design page ..see below..

                        %@ Page Language="C#" AutoEventWireup="true" CodeFile="MedPending.aspx.cs" Inherits="Maintenance_MedPending" %>

                        Untitled Page
                        

                        :confused:

                        I Offline
                        I Offline
                        Ibrahim Bello
                        wrote on last edited by
                        #11

                        If this suggestion helped you, kindly say so, so the thread will be marked as resolved.

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups