Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

867 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

16 Experts available now in Live!

Get 1:1 Help Now