Solved

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

Posted on 2006-11-16
4
401 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
[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
  • 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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

Suggested Solutions

This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

733 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