Solved

ReadXML - datagridview only show certain columns or change header Text?

Posted on 2006-11-16
4
393 Views
Last Modified: 2008-01-09
Hello,
I am using ReadXML to load a dataset and then use a datagridview table.

How do I only display 1 or 2 columns in that datagridview instead of all the columns? and also to change the header text?

all this is on page load

thanks!

        private void viewStudents_Load(object sender, EventArgs e)
        {
            studentDataSet.ReadXml("C:\\StudentList.xml");
           
            studentGridView.DataSource = studentDataSet;

            studentGridView.DataMember = "student";
           
           
        }
0
Comment
Question by:JasonWinn
  • 2
  • 2
4 Comments
 
LVL 3

Expert Comment

by:bromose
Comment Utility
Hi

Add this function:

        private void DoDataGridViewSetup(int[] showing, string[] headerText)
        {
            DataGridViewColumnCollection gridColums = studentGridView.Columns;
           
            foreach (DataGridViewColumn column in gridColums)
            {
                column.Visible = false;
            }

            for (int i=0; i < showing.Length; i++)
            {

                gridColums[showing[i]].HeaderText = headerText[i];
                gridColums[showing[i]].Visible = true;
            }
        }

As parameter You give an array containing the columns you want to show - fx. nr 0 and nr 4.
Also You parse a string array containing the header text you want for the columns to show.

I beleve this solves your problem.

Best Regards,
Kaj Bromose
0
 
LVL 3

Author Comment

by:JasonWinn
Comment Utility
Kaj,
I will give that a try soon as I get him tonight and will let you know. Looking at the code though, it seems like it will solve it.

Thank you!
0
 
LVL 3

Author Comment

by:JasonWinn
Comment Utility
Hey Kaj,

I added the code as a seperate function. however, how do I call the function?

private void viewStudents_Load(object sender, EventArgs e)
        {
            studentDataSet.ReadXml("C:\\StudentList.xml");
           
            studentGridView.DataSource = studentDataSet;

            studentGridView.DataMember = "student";
           
        }

        private void DoDataGridViewSetup(int[] showing, string[] headerText)
        {
            DataGridViewColumnCollection gridColums = studentGridView.Columns;

            foreach (DataGridViewColumn column in gridColums)
            {
                column.Visible = false;
            }

            for (int i = 0; i < showing.Length; i++)
            {

                gridColums[showing[i]].HeaderText = headerText[i];
                gridColums[showing[i]].Visible = true;
            }
        }
0
 
LVL 3

Accepted Solution

by:
bromose earned 500 total points
Comment Utility
Hi Jason

If You always are interrested in  - say column 2 and 4, and the headings should be Name and Phone Number. You could change the code to:

private void viewStudents_Load(object sender, EventArgs e)
        {
            studentDataSet.ReadXml("C:\\StudentList.xml");
           
            studentGridView.DataSource = studentDataSet;

            studentGridView.DataMember = "student";
 

           int[] showing = {2,4};
           string[] heading = {"Name","Phone Number"};
           DoDataGridViewSetup(showing, heading);
           
        }

        private void DoDataGridViewSetup(int[] showing, string[] headerText)
        {
            DataGridViewColumnCollection gridColums = studentGridView.Columns;

            foreach (DataGridViewColumn column in gridColums)
            {
                column.Visible = false;
            }

            for (int i = 0; i < showing.Length; i++)
            {

                gridColums[showing[i]].HeaderText = headerText[i];
                gridColums[showing[i]].Visible = true;
            }
        }


Best Regards,
Kaj Bromose
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

763 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now