Solved

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

Posted on 2006-11-16
4
399 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
ID: 17963058
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
ID: 17965417
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
ID: 17972917
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
ID: 17974427
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

792 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