473,396 Members | 2,024 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Display a datagridview with a certain columns when web application runs

Hi all,

I have some following code:

Expand|Select|Wrap|Line Numbers
  1. DataTable dt = m_Data.GetInfo();
  2.  
  3. GridView1.DataSource = dt;
  4.  
  5.  GridView1.DataBind();
  6.  
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:
Expand|Select|Wrap|Line Numbers
  1. //*******my Data.class*************//
  2.  
  3.  public DataTable GetInfo()
  4.         {
  5.             DataTable dt = new DataTable();
  6.  
  7.             try
  8.             {
  9.                 // Create command
  10.                 // Command must use two part names for tables
  11.                 // SELECT <field> FROM dbo.Table rather than 
  12.                 // SELECT <field> FROM Table
  13.                 // Query also can not use *, fields must be designated
  14.                 SqlCommand cmd = new SqlCommand("sp_GetInfo", m_sqlConn);
  15.                 cmd.CommandType = CommandType.StoredProcedure;
  16.  
  17.  
  18.  
  19.                 // Open the connection if necessary
  20.                 if(m_sqlConn.State == ConnectionState.Closed)
  21.                     m_sqlConn.Open();
  22.  
  23.                 // Get the messages
  24.                 dt.Load(cmd.ExecuteReader(CommandBehavior.CloseConnection));
  25.             }
  26.             catch (Exception ex)
  27.             {
  28.                 throw ex;
  29.             }
  30.  
  31.             return dt;
  32.         }
  33.  
  34. //........................................................................................
  35.  
  36. //****my page*****//
  37.  
  38. DataTable dt = m_Data.GetInfo();
  39.  
  40. GridView1.DataSource = dt;
  41.  
  42.  GridView1.DataBind();
  43.  
  44.  
Have you got any suggestion for me?

Thank kiu so much!
Nov 30 '09 #1

✓ answered by Frinavale

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

8 6806
Frinavale
9,735 Expert Mod 8TB
It's actually easier to do than you think.
In your GridView control you just specify which columns to bind to:
Expand|Select|Wrap|Line Numbers
  1. <asp:GridView ID="MyGridView" runat="server" AutoGenerateColumns="False">
  2.   <Columns>
  3.     <asp:BoundField DataField="A">
  4.     </asp:BoundField>
  5.     <asp:BoundField DataField="B">
  6.     </asp:BoundField>
  7.     <asp:BoundField DataField="C">
  8.     </asp:BoundField>
  9.   <Columns>
  10. </asp:GridView>
  11.  
-Frinny
Nov 30 '09 #2
Thank kiu! I have done it by the following way:
Expand|Select|Wrap|Line Numbers
  1.      if (GridView1.Columns.Count > 0) 
  2.             GridView1.Columns[0].Visible = false;
  3.         else
  4.         { 
  5.             GridView1.HeaderRow.Cells[0].Visible = false; 
  6.  
  7.            foreach (GridViewRow gvr in GridView1.Rows)
  8.             {
  9.                 gvr.Cells[2].Visible = false; 
  10.            }
  11.         }
Dec 1 '09 #3
Frinavale
9,735 Expert Mod 8TB
:) That works too :)

Glad you found your answer

-Frinny
Dec 1 '09 #4
sanjib65
102 100+
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!
Dec 2 '09 #5
Frinavale
9,735 Expert Mod 8TB
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
Dec 2 '09 #6
sanjib65
102 100+
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.
Dec 3 '09 #7
Thanks Frinavale! .... ^.^
Dec 3 '09 #8
Excellent. Easy and very useful.
Jan 18 '16 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: manjub | last post by:
Hi, I was working on an MDI application using Visual C++ 2005 .NET. In a child window, I have an instance of DataGridView control. If I open several child windows, they all work fine. ...
8
by: George | last post by:
Hi, I have been trying to see if I can do the following: 1. Create a DataGridView 2. Create 2 columns in the DataGridView (1 textbox and the other combobox) 3. Create a DataTable containing...
0
by: Mike | last post by:
Hey everyone... I've got three problems with a custom DataGridView column I've built following the "How To: Host Controls in Windows Forms DataGridView Cells" article. The base editing control...
3
by: Max | last post by:
Hello, I have a datagridview object on my form along with some other controls. When the application runs, the datagridview is populated with data from a database. Although the right number of...
3
by: Claes Wedin | last post by:
I'm doing a Component class that inherits from DataGridView. One thing that it does is to add a default column in the grid. I put the code for this in the class and when I drag the control to my...
0
OuTCasT
by: OuTCasT | last post by:
Hi all. i got a student/product management application. now on the main page there is a datagridview with 4 columns, Name | Description | Qty | Price...
1
by: TG | last post by:
Hi! I have an application in which I have some checkboxes and depending which ones are checked those columns will show in the datagridview from sql server or no. After that I have 2 buttons:...
2
by: TG | last post by:
Hi! I am trying to export only the visible columns from a datagridview in my windows form in VB 2008. Should't it be no comma after the first row where the headers are? Also should...
6
by: Ciaran | last post by:
I'm having a really strange issue with the DataGridView control in a VS2008 / .NET 3.5 winforms project. I have a simple form with a grid. In the form constructor I call a function to bind the grind...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.