Solved

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

Posted on 2006-11-16
4
398 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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

831 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