Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

c#, undound datagrid, header text

Posted on 2007-11-19
8
Medium Priority
?
310 Views
Last Modified: 2010-04-21
how can I change the header text on an unbound datagrid that has columns dynamically generated at runtime?  I am using the attached code to create the table , but the dynamically generated columns only come up with the column name for the header.
private DataTable datbl()
		{
			DataTable tbl = new DataTable("X");
			DataColumn Coldt;
			DataRow Rowdt; 
			Coldt = new DataColumn();
			Coldt.DataType= System.Type.GetType("System.Int32");
			Coldt.ColumnName = "ID";
			Coldt.AutoIncrement = true;
			Coldt.Caption = "EMPID";
			Coldt.ReadOnly = true;
			Coldt.Unique = true;
 
			tbl.Columns.Add(Coldt);
 
			Coldt = new DataColumn();
			Coldt.DataType= System.Type.GetType("System.String");
			Coldt.ColumnName = "FName";
			Coldt.AutoIncrement = false;
			Coldt.Caption = "Employee First Name";
			Coldt.ReadOnly = false;
			Coldt.Unique = false;
			tbl.Columns.Add(Coldt);
 
			Coldt = new DataColumn();
			Coldt.DataType= System.Type.GetType("System.String");
			Coldt.ColumnName = "LName";
			Coldt.AutoIncrement = false;
			Coldt.Caption = "Employee Last Name";
			Coldt.ReadOnly = false;
			Coldt.Unique = false;
			tbl.Columns.Add(Coldt);
 
			Rowdt = tbl.NewRow();
			Rowdt["ID"] = 0001;
			Rowdt["FName"] = "Kasi";
			Rowdt["LName"] = "Viswanath" ;
			tbl.Rows.Add(Rowdt);
 
			Rowdt = tbl.NewRow();
			Rowdt["ID"] = 0002;
			Rowdt["FName"] = "Arun";
			Rowdt["LName"] = "Kumar" ;
			tbl.Rows.Add(Rowdt);
			
			Rowdt = tbl.NewRow();
			Rowdt["ID"] = 0003;
			Rowdt["FName"] = "Srivasan";
			Rowdt["LName"] = "Ramachandran" ;
			tbl.Rows.Add(Rowdt);
 
			return tbl;
 
			
		}
 
//Draw Table
bindtbl  = datbl();
Session["DataTable"] = bindtbl;
DataGrid1.DataSource = Session["DataTable"];
DataGrid1.DataBind();

Open in new window

0
Comment
Question by:soltan8
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 12

Expert Comment

by:topdog770
ID: 20313311
Here's a couple of ideas.

1) Use the designer to add unbound datacolumns to the datagridview.  Set the header text with the name(s) you are wanting and set the DataPropertyName to match the column names you set in the code above.  You'd have DataPropertyNames of ID, LName, etc

2) In your code, you can also do the same process I've layed out above.  Create Columns for the DataGridView and set the DataPropertyNames
0
 

Author Comment

by:soltan8
ID: 20313495
I probablly should have mentioned that this is .net 1.1 using VS 2003.  I don't think I have the option of using a DataGridView(At least I don't see it as an option).  If I'm wrong, please give me an example of how to use it.. Thanks
0
 
LVL 12

Expert Comment

by:topdog770
ID: 20313570
My fault, I should have scrolled down the code.


void FormatColumns()
		{
			DataGridTableStyle dgTableStyle = new DataGridTableStyle();
			dgTableStyle.MappingName = "X"; // name of data table
 
			DataGridColumnStyle dgColumnStyle = new DataGridTextBoxColumn();
			dgColumnStyle.MappingName = "ID";
			dgColumnStyle.HeaderText = "EMPID";
			dgColumnStyle.Width = 45;
			dgTableStyle.GridColumnStyles.Add(dgColumnStyle);
 
			dgColumnStyle = new DataGridTextBoxColumn();
			dgColumnStyle.MappingName = "FName";
			dgColumnStyle.HeaderText = "Employee First Name";
			dgColumnStyle.Width = 120;
			dgTableStyle.GridColumnStyles.Add(dgColumnStyle);
 
			dgColumnStyle = new DataGridTextBoxColumn();
			dgColumnStyle.MappingName = "LName";
			dgColumnStyle.HeaderText = "Employee Last Name";
			dgColumnStyle.Width = 120;
			dgTableStyle.GridColumnStyles.Add(dgColumnStyle);
 
			DataGrid1.TableStyles.Add(dgTableStyle);
		}

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 12

Expert Comment

by:topdog770
ID: 20313578
Call or use the functionality, then do the data binding
0
 

Author Comment

by:soltan8
ID: 20313954
it is failing on DataGrid1.TableStyles.Add(dgTableStyle); This is a web form and apparently DataGrid1 does not have a TableStyles Property.  Any other ideas?
0
 
LVL 12

Accepted Solution

by:
topdog770 earned 500 total points
ID: 20314437
Sorry, I'm really slow today.

In your aspx page configure the datagrid like this

The Key is Autogeneratecolumns = false;

Then you have to add each column you want visible.  I just changed the column header text, but you can configure formatting for dates, etc here as well.
			<asp:DataGrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 112px; POSITION: absolute; TOP: 200px"
				runat="server" ForeColor="#FF8080" AutoGenerateColumns=False>
				<Columns>
				             <asp:BoundColumn DataField="ID"
HeaderText="EMPID"></asp:BoundColumn>
 
                    <asp:BoundColumn DataField="FName"
HeaderText="First Name"></asp:BoundColumn>
 
                    <asp:BoundColumn DataField="LName"
HeaderText="Last Name"></asp:BoundColumn>
 
                </Columns>
				
				</asp:DataGrid>

Open in new window

0
 

Author Closing Comment

by:soltan8
ID: 31409954
Thanks so much!! That is exactly what I needed.
0
 
LVL 12

Expert Comment

by:topdog770
ID: 20315366
Thank you for the A, soltan8
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses

660 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question