datagridview xml file.

conceptdata
conceptdata used Ask the Experts™
on
Hi.

I have a simple windows app. with a datagridview, which I populate from a XML file.
See the Code ..

I would like to edit/add/remove columns, but can't do that. (visual studio 2008)
any suggestions .. ?


THANKS




try
            {
                XmlDataDocument xmldata = new XmlDataDocument();
                xmldata.DataSet.ReadXml(Application.StartupPath + "\\Automatisering.xml");

                dataGridView1.DataSource = xmldata.DataSet;
                dataGridView1.DataMember = "AccountName";
            }
            catch (Exception)
            {
                MessageBox.Show("Error");
            }

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
check:
dataGridView1.Columns.Add("ColName", "Col Header"); //Add
                dataGridView1.Columns["ColName"].HeaderText = "New Text"; //Edit
                dataGridView1.Columns.Remove("ColName") //Delete

Open in new window

Author

Commented:
ok, and thanks. I can se that works fine..

BUT, what I ment, was edit,remove and Add through the "DataGridView Tasks" box.
In visual studio 2008..
Sorry

??

Author

Commented:
OWASP: Forgery and Phishing

Learn the techniques to avoid forgery and phishing attacks and the types of attacks an application or network may face.

ok

from that pic you sent me -

click on edit columns

on the next screen click add ... at the bottom left hand side. you can then give it a name and headertext and type. - this is how you add

from the previous screen you can now selkect the column and use remove to delete and the properties on the right to edit :)

Author

Commented:
Stiill the same problem here.
See attached picture.
I have no datasource, and can't choose it.
Therefore I don't have any Columns fra the XML-file in the Add/Edit Columns...
????
01-02-2010-08-33-32.jpg
set it up like unbound, and when you bind it later it will readjust.

Author

Commented:
I'm a little bit newbie in this.

Can you give me a hint HOW ?
As you have the colum there in the comment id: 26452553

Just add the Name and Header Text.

As per my previous example:

dataGridView1.Columns.Add("ColName", "Col Header"); //Add

ColName == Name field and
Header Text = Col Header

Author

Commented:
I have a field in my XML named : Copy
I add a new field in my Grid named : CopyCheck

How do I bind thoose together .. ?
ok, please attach the xml here (a sampple only) and I will write it for you.

Author

Commented:
atteched - thanks

Author

Commented:
atteched - thanks
Automatisering.xml
what columns do you want displayed ?

Author

Commented:
     <Name>F01</Name>
      <Copy/>       // CHECKBOX updates at writing XML back to file with 1 og 0
      <CompagnyName></CompagnyName>
      <Template/>

Author

Commented:
Any news ?
sorry for the delay - had some issues this side ... will post a colution  shortly ;)
 

Author

Commented:
Thanks :)

Author

Commented:
Any Solution :-)
?
yea.

OK.
Step 1: in the design view right click on the datagridview control and select edit columns.

Step 2: click the add button on the bottom left of the edit columns dialog.

Step 3: enter in a Name and HeaderTex (NB: Don't use the name [Name] - as this step creates a control with that name and form1 has a property called name as well - will cause compile error. This is a bug in the control. So call it xmlName or xmlWhatever. The header Text can be called Name) and click Add and then ok.

Step 4: Select the column in the listbox on the right hand side and then on the left hand side set the property [DataPropertyName] to Name (ie: field that you want to display)

Step 5 Click OK.

Now go to the code view where you want to load the data (I am going to do it from form_Load) and add the code as shown below.

private static void LoadXmlDataGrid(ref DataGridView grid, string xmlFile, string tableName)
        {
            //first we have to set this property to false so that no all the columns 
            //are added to the grid view.
            grid.AutoGenerateColumns = false;

            var xmldata = new XmlDataDocument();
            xmldata.DataSet.ReadXml(xmlFile);
            grid.DataSource = xmldata.DataSet == null ? null : xmldata.DataSet.Tables[tableName];
        }


        private void Form1_Load(object sender, EventArgs e)
        {
            LoadXmlDataGrid(ref dataGridView1, "c:\\Automatisering.xml", "AccountName");
        }

Open in new window

Author

Commented:
Fine - Great Work ....
Thanks...

Can you help me makin a Save button, and a "linked" GridView With other data from the Accounts TAG.

Author

Commented:
The save button must Update the XML-file with the new type data.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial