Display a datagridview with a certain columns when web application runs

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Mai Phuong
    New Member
    • Sep 2009
    • 25

    Display a datagridview with a certain columns when web application runs

    Hi all,

    I have some following code:

    Code:
    DataTable dt = m_Data.GetInfo();
    
    GridView1.DataSource = dt;
    
     GridView1.DataBind();
    Table dt, in fact, has 5 columns A, B, C, D, E, but I want to display just 3 columns in datagridviews A, B, C with new names A1, B1, C1. And I do not know how to do.

    Notice that this can not be done in the designing step because my datasoucre is unknown until application runs. It is got by the following code:
    Code:
    //*******my Data.class*************//
    
     public DataTable GetInfo()
            {
                DataTable dt = new DataTable();
    
                try
                {
                    // Create command
                    // Command must use two part names for tables
                    // SELECT <field> FROM dbo.Table rather than 
                    // SELECT <field> FROM Table
                    // Query also can not use *, fields must be designated
                    SqlCommand cmd = new SqlCommand("sp_GetInfo", m_sqlConn);
                    cmd.CommandType = CommandType.StoredProcedure;
    
                            
    
                    // Open the connection if necessary
                    if(m_sqlConn.State == ConnectionState.Closed)
                        m_sqlConn.Open();
    
                    // Get the messages
                    dt.Load(cmd.ExecuteReader(CommandBehavior.CloseConnection));
                }
                catch (Exception ex)
                {
                    throw ex;
                }
    
                return dt;
            }
    
    //........................................................................................
    
    //****my page*****//
    
    DataTable dt = m_Data.GetInfo();
    
    GridView1.DataSource = dt;
    
     GridView1.DataBind();
    Have you got any suggestion for me?

    Thank kiu so much!
    Last edited by Frinavale; Nov 30 '09, 03:15 PM. Reason: Please post code in [code] ... [/code] tags. Changed quote tags into code tags.
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    It's actually easier to do than you think.
    In your GridView control you just specify which columns to bind to:
    Code:
    <asp:GridView ID="MyGridView" runat="server" AutoGenerateColumns="False">
      <Columns>
        <asp:BoundField DataField="A">
        </asp:BoundField>
        <asp:BoundField DataField="B">
        </asp:BoundField>
        <asp:BoundField DataField="C">
        </asp:BoundField>
      <Columns>
    </asp:GridView>
    -Frinny

    Comment

    • Mai Phuong
      New Member
      • Sep 2009
      • 25

      #3
      Thank kiu! I have done it by the following way:
      Code:
           if (GridView1.Columns.Count > 0) 
                  GridView1.Columns[0].Visible = false;
              else
              { 
                  GridView1.HeaderRow.Cells[0].Visible = false; 
                  
                 foreach (GridViewRow gvr in GridView1.Rows)
                  {
                      gvr.Cells[2].Visible = false; 
                 }
              }

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        :) That works too :)

        Glad you found your answer

        -Frinny

        Comment

        • sanjib65
          New Member
          • Nov 2009
          • 102

          #5
          I'd like to stretch this thread slightly.
          Is it really neccessary to do this through coding?
          Yes, conceptually you've to learn that way! But in real world is it at all needed?
          The whole thing has been made extremely easy If I use Wizard. In the frmawork GridView Control it will need few clicks to choose the Cells!

          Comment

          • Frinavale
            Recognized Expert Expert
            • Oct 2006
            • 9749

            #6
            No, it's not necessary to do this through C# code. You could do this through your ASP code for the GridView Columns.

            You could do as I suggested and only have the GridView bind to certain columns of the data source.

            Or you could specify all of the GridView columns and also specify a CSS style that hides/shows/styles the columns in a specific manner. You could specify this in your ASP code or you could do this in the C# code (sometimes you have to actually).

            Or you could create a SQL query/stored procedure that only returns the columns that you want to display (this would probably take the least amount of time if the query is simple).


            There are a lot of ways to accomplish this. It doesn't necessarily have to be done the way that the original poster settled on. It all depends on your data source, your application requirements, how you create your data source, how the data source has to be displayed, and what you need to use for calculations based on the data source. And I think the biggest thing that it depends on is your specific programming style (each programmer has a unique style)

            The thing about using a wizard is that it generates code that will work to get you started; however, it is very common for you to need to edit what the wizard has done anyways. I personally do not like to use the wizards. I prefer to do things in my server side code because I like to have control over what is done with the data source instead of letting some other pre-baked datasource-control have control over what is done with the data source.


            -Frinny

            Comment

            • sanjib65
              New Member
              • Nov 2009
              • 102

              #7
              Very nice of you to explain things in detail. As a lazy person I always inclined to drag and drop of kind of things, wizard does this actually. But that way I found it really tough to manipulate data later in the code behind page.
              Your vision encouraged me to go for the server side coding as much as possible.

              Comment

              • Mai Phuong
                New Member
                • Sep 2009
                • 25

                #8
                Thanks Frinavale! .... ^.^

                Comment

                • SalmanMushtaq
                  New Member
                  • Jan 2016
                  • 3

                  #9
                  Excellent. Easy and very useful.

                  Comment

                  Working...