using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Xml;

namespace Shortcuts
{
    public partial class Form1 : Form
    {               
        public Form1()
        {
        InitializeComponent();
        }

        public string FileLocationToRun;

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog fdlg = new OpenFileDialog();
            fdlg.Title = "Browse";
            //fdlg.InitialDirectory = System.Environment.GetFolderPath(Environment.SpecialFolder.Recent);
            fdlg.Filter = "All Files.* (*.*)|*.*";
            fdlg.FilterIndex = 2;
            fdlg.RestoreDirectory = true;
            string fullpath = "";
            if (fdlg.ShowDialog() == DialogResult.OK)
            {
                fullpath = System.IO.Path.GetFullPath(fdlg.FileName);
                textBox1.Text = fullpath;
            }
        }

        private void button2_Click_1(object sender, EventArgs e)
        {
            if (textBox1.Text.Equals("") | textBox2.Text.Equals(""))
            {
                MessageBox.Show("One of the required fields does not contain any text", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            
            else
            {          
                dataGridView1.Rows.Add(textBox2.Text, textBox1.Text);                
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            FileLocationToRun = dataGridView1.SelectedCells[1].Value.ToString();
            Process.Start(FileLocationToRun);
        }
        
        private void button4_Click(object sender, EventArgs e)
        {


        }

        private void button5_Click(object sender, EventArgs e)
        {
          
        }
    }
}

thats my code so far, i need the datagridview to be able to save and load from a small file that i can preferably embed within the program.

Any help ?

thanks,

Recommended Answers

All 29 Replies

Can you be please a bit more splecific. What exactly do you need.
And the code above you`ve posted, its not in any help - it has nothing to do with your question. So please concentrate on what you need, and if you want some help from us, please, explain your needs (precisly).
Because now I can only guess and do 1000 of lines of code only for your and on the end will all be worthless. I hope you understand, so please do your best.
bye,

Mitja

I suggest you to use DataSet. Methods of DataSet; WriteXml and ReadXml are used to store/load data into/from xml document.

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        DataSet ds = new DataSet("Info");
        DataTable dt = new DataTable("Record");


        string xsd = @"c:\cs\sample.xsd";
        string xml = @"c:\cs\sample.xml";
        private void Form1_Load(object sender, EventArgs e)
        {
            if (!File.Exists(xsd))
            {
                dt.Columns.Add("Path");
                dt.Columns.Add("Value");
                ds.Tables.Add(dt);

                ds.WriteXmlSchema(xsd);
                ds.WriteXml(xml);
            }
            else
            {
                ds.ReadXmlSchema(xsd);
                ds.ReadXml(xml);
            }

            //Binds datagrid
            dataGridView1.DataSource = ds;
            dataGridView1.DataMember = "Record";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            
           ds.Tables["Record"].Rows.Add(textBox1.Text, textBox2.Text);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog();
            textBox1.Text = openFileDialog1.FileName;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //Write data
            ds.WriteXml(xml);
        }
    }

Hope this help you.

PS: ok some understanding from me and some help for you, that will be a bit easier to explain:
you have a dgv. With columns.
- What exactly are these columns?
- What exactly are the data in these columns?
- What would be the file like (whihch type - txt?) to write into and read from it?
- How and when the dgv needs to be populated (and refreshed - if ever)?
- and some your important info

bye,

Mitja

I Hope this Will Help You

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using SRS.BO;
using System.Data.SqlClient;

namespace SRS_GUI
{
    public partial class Add : Form
    {
        public Add()
        {
            InitializeComponent();
        }
       
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                Student objStudents = new Student(txtid.Text,txtname.Text,txtmajor.Text,txtDegree.Text);//new User Object
                objStudents.AdviseBy = (SRS.BO.Professor)cmbAdvisor.SelectedItem;
                         

                
 
                bool  result = objStudents.AddNewStudent(Main.connectionString,objStudents); //DBHelper.ExecuteNonQueryDB(query, connectionString);


                if (result==true )
                {
                    LabelStatusStudent.Text = "Added";
                    LoadUsers();
                }
                else
                {
                    
                    LabelStatusStudent.Text = "Failed To Add";
                 
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }

        private void LoadUsers()
        {
            try 
            {
                DataSet objDataSet = new DataSet();
                objDataSet = studentManager.GetStudentList(Main.connectionString);
                DGVStudents.DataSource = objDataSet.Tables[0];

                DGVStudents.Columns[2].HeaderText = "Student ID";
                DGVStudents.Columns[3].HeaderText = "Name";
                DGVStudents.Columns[4].HeaderText = "Major";
                DGVStudents.Columns[5].HeaderText = "Degree";
                

            }
            catch (Exception ex) 
            { throw ex; }
        }
        private void Add_Load(object sender, EventArgs e)
        {
            LoadUsers();
            LoadAdvisor();
        }
        #region Data Grid Buttons
        private void DGVStudents_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int ColumnIndex = e.ColumnIndex;
            int RowIndex = e.RowIndex;
            if (RowIndex != -1)
            {

                if (ColumnIndex == 0)
                {


                    DialogResult dialogresult = MessageBox.Show("Are You Sure You Want To Delete", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);


                    if (dialogresult == DialogResult.Yes)
                    {
                        try
                        {
                            Student objDeleteStudent = new Student(DGVStudents[2, RowIndex].Value.ToString(), DGVStudents[3, RowIndex].Value.ToString(), DGVStudents[4, RowIndex].Value.ToString(), DGVStudents[5, RowIndex].Value.ToString());
                           

                            objDeleteStudent.DeleteStudent(Main.connectionString, objDeleteStudent);
                            LoadUsers();
                            MessageBox.Show("Deleted");
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                    }
                }
                else if (ColumnIndex == 1)
                {
                    //Edit
                    txtid.Text = DGVStudents[2, RowIndex].Value.ToString();
                    txtname.Text = DGVStudents[3, RowIndex].Value.ToString();
                    txtmajor.Text = DGVStudents[4, RowIndex].Value.ToString();
                    txtDegree.Text = DGVStudents[5, RowIndex].Value.ToString();
                    cmbAdvisor.SelectedValue = DGVStudents[6, RowIndex].Value.ToString();

                }
            }
        }
        #endregion



        private void btnUpdate_Click(object sender, EventArgs e)
        {
            try 
            {
                if (ValidateControl.IsNotEmpty(txtid))
                {
                    if (ValidateControl.IsNotEmpty(txtname))
                    {
                        if (ValidateControl.isNumber(txtid))
                        {
                            if (ValidateControl.IsNotEmpty(txtmajor))
                            {
                                if (ValidateControl.IsNotEmpty(txtDegree))
                                {
                                    #region UpdateCode
                                    Student studentsobj = new Student(txtid.Text, txtname.Text, txtmajor.Text, txtDegree.Text);
                                    studentsobj.AdviseBy = (SRS.BO.Professor)cmbAdvisor.SelectedItem;
                                    ErrPrrName.SetError(txtid, "");
                                    ErrPrrName.SetError(txtname, "");
                                    ErrPrrName.SetError(txtmajor, "");
                                    ErrPrrName.SetError(txtDegree, "");

                                    if (studentsobj.UpdateStudent(Main.connectionString, studentsobj))
                                    {
                                        MessageBox.Show("Updated");
                                        LoadUsers();
                                    }
                                    else
                                    {
                                        MessageBox.Show("Failed To Update");
                                    }
                                    #endregion
                                }
                                else
                                {
                                    ErrPrrName.SetError(txtDegree, "Please Enter Degree");

                                }
                            }
                            else
                            {
                                ErrPrrName.SetError(txtmajor, "Please Enter Major");
                            }



                        }
                        else
                        {
                            ErrPrrName.SetError(txtid, "Please Enter Integer Value");
                        }

                    }
                    else
                    {
                        ErrPrrName.SetError(txtname, "Please Enter Name");
                    }
                }
                else
                {
                    ErrPrrName.SetError(txtid, "Please Enter ID");
                }

            }
            catch (Exception ex) 
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void LoadAdvisor()
        {
            try
            {
                cmbAdvisor.DataSource = ProfessorManager.GetAdvisorList(Main.connectionString);
                cmbAdvisor.ValueMember = "ID";
                cmbAdvisor.DisplayMember = "Name";


            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.ToString());
            }
        }

        private void cmbAdvisor_SelectedIndexChanged(object sender, EventArgs e)
        {
            labelResult.Text = cmbAdvisor.SelectedItem.ToString();
            labelID.Text = cmbAdvisor.SelectedValue.ToString();
            lblName.Text = cmbAdvisor.Text;
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
           
        }




    }
}

Creating a DataSet

public static DataSet GetStudentList(string connection)
        {
            try
            {
                string query = "select * from student";
                return DBHelper.GetDateSet(query, connection);

            }
            catch (Exception ex)
            { throw ex; }
        }

Thanks guys, i will try these when i get home. Will update soon,

I suggest you to use DataSet. Methods of DataSet; WriteXml and ReadXml are used to store/load data into/from xml document.

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        DataSet ds = new DataSet("Info");
        DataTable dt = new DataTable("Record");


        string xsd = @"c:\cs\sample.xsd";
        string xml = @"c:\cs\sample.xml";
        private void Form1_Load(object sender, EventArgs e)
        {
            if (!File.Exists(xsd))
            {
                dt.Columns.Add("Path");
                dt.Columns.Add("Value");
                ds.Tables.Add(dt);

                ds.WriteXmlSchema(xsd);
                ds.WriteXml(xml);
            }
            else
            {
                ds.ReadXmlSchema(xsd);
                ds.ReadXml(xml);
            }

            //Binds datagrid
            dataGridView1.DataSource = ds;
            dataGridView1.DataMember = "Record";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            
           ds.Tables["Record"].Rows.Add(textBox1.Text, textBox2.Text);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog();
            textBox1.Text = openFileDialog1.FileName;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //Write data
            ds.WriteXml(xml);
        }
    }

Hope this help you.

i get an error on:

private void button1_Click(object sender, EventArgs e)
{
ds.Tables["Record"].Rows.Add(textBox1.Text, textBox2.Text);
}

my xml file contains this right now:

<?xml version="1.0" encoding="utf-8" ?>
<Data>
  <Row>
    <FileName> test </FileName>
    <FileLocation> test </FileLocation>  
  </Row>  
</Data>

xsd file:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="xmlfile"
    targetNamespace="http://tempuri.org/xmlfile.xsd"
    elementFormDefault="qualified"
    xmlns="http://tempuri.org/xmlfile.xsd"
    xmlns:mstns="http://tempuri.org/xmlfile.xsd"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
</xs:schema>

what my program enables the user to do is that he/she can use it as a shortcut to loading other programs. I know you can just make a folder but this makes it much more clear. My program allows the user to browse for a file then make a name for it, then it adds the two user inputted values in a datagridview and when the user highlights the row that shows the program he loads and he presses the load button the program will load. Now i can do all that but i when i exit the program all the data is lost. I need it to be saved, i am going to try that by means of an XML file.

any help with my coding ?

i get an error on:

private void button1_Click(object sender, EventArgs e)
{
    ds.Tables["Record"].Rows.Add(textBox1.Text, textBox2.Text);
}

You didnt specify to which column of the dataTable belong values form textBox1 and textBox2.
btw, do you intend to insert both values into one column, or both values in seperated columns?

Otherwise, inserting values into columns of dataTabele of dataSet goes like this:

private void PopulateDataSet()
        {
            using (DataSet ds = new DataSet())
            {
                using (DataTable table = new DataTable("1stTable"))
                {
                    DataRow newRow;
                    //creating column of the dataTable:
                    table.Columns.Add(new DataColumn("Column1", typeof(string)));
                    table.Columns.Add(new DataColumn("Column2", typeof(string)));
                                        
                    string value1 = "valuue1"; //use textBox1.Text
                    string value2 = "value2";  //use textBox2.Text
                    //and so on...
                    
                    string[] array = new string[] { value1, value2 };
                    int column;

                    for (int i = 0; i < array.Length; i++)
                    {
                        column = 0;
                        newRow = table.NewRow();
                        newRow["Column1"] = array[i];
                        column++;
                        newRow["Column2"] = array[i];
                        //adding row into dataTable:
                        table.Rows.Add(newRow);
                    }
                    //adding dataTable to dataSet:
                    ds.Tables.Add(table);
                }
            }
        }

But I would adivse you not to use dataSet, if you dont have more then 1 dataTables at ones to use. It will only complicate things.

Hope it helps,
if questions, please ask,

Mitja

i have two columns, the first is called FileName, and the 2nd FileLocation

I know how to add data to them, i just want to save the data in the datagridview on exit and load the data back when the program is ran again.

any ideas?

You didnt specify to which column of the dataTable belong values form textBox1 and textBox2.
btw, do you intend to insert both values into one column, or both values in seperated columns?

Otherwise, inserting values into columns of dataTabele of dataSet goes like this:

private void PopulateDataSet()
        {
            using (DataSet ds = new DataSet())
            {
                using (DataTable table = new DataTable("1stTable"))
                {
                    DataRow newRow;
                    //creating column of the dataTable:
                    table.Columns.Add(new DataColumn("Column1", typeof(string)));
                    table.Columns.Add(new DataColumn("Column2", typeof(string)));
                                        
                    string value1 = "valuue1"; //use textBox1.Text
                    string value2 = "value2";  //use textBox2.Text
                    //and so on...
                    
                    string[] array = new string[] { value1, value2 };
                    int column;

                    for (int i = 0; i < array.Length; i++)
                    {
                        column = 0;
                        newRow = table.NewRow();
                        newRow["Column1"] = array[i];
                        column++;
                        newRow["Column2"] = array[i];
                        //adding row into dataTable:
                        table.Rows.Add(newRow);
                    }
                    //adding dataTable to dataSet:
                    ds.Tables.Add(table);
                }
            }
        }

But I would adivse you not to use dataSet, if you dont have more then 1 dataTables at ones to use. It will only complicate things.

Hope it helps,
if questions, please ask,

Mitja

i have two columns, the first is called FileName, and the 2nd FileLocation

I know how to add data to them, i just want to save the data in the datagridview on exit and load the data back when the program is ran again.

any ideas?

ps, i dont want to use things like SQL connection and things like that since the user might not be able to use it (i am going to share my program)

If you have 2 columns in the dataTable then to populate dgv simpe create a method and call it (in the method bellow I pass the parameter of dataSet):

private void PopulatringDGV(DataSet ds)
        {
            int row = 0;
            int column = 0;
            foreach (DataRow dr in ds.Tables[0].Rows) //or: Tables["TableName"]
            {
                column = 0;
                this.dgv[column, row].Value = dr[0].ToString();
                column++;
                this.dgv[column, row].Value = dr[1].ToString();
                row++;
            }
        }

If you have 2 columns in the dataTable then to populate dgv simpe create a method and call it (in the method bellow I pass the parameter of dataSet):

private void PopulatringDGV(DataSet ds)
        {
            int row = 0;
            int column = 0;
            foreach (DataRow dr in ds.Tables[0].Rows) //or: Tables["TableName"]
            {
                column = 0;
                this.dgv[column, row].Value = dr[0].ToString();
                column++;
                this.dgv[column, row].Value = dr[1].ToString();
                row++;
            }
        }

i get the error:

Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

Show me the code, and point on the row where the error occures?
Your problem is that you try to fill the data into uncreated columns or rows.

Show me the code, and point on the row where the error occures?
Your problem is that you try to fill the data into uncreated columns or rows.

im basically stuck =[

I dont get this,

all i want to do is to load the data from a xml file to a datagridview & the ability to save the data into an xml file

Ill show you my code so far please assist me:

form1.cs:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Xml;
using System.IO;
using System.Xml.Linq;

namespace Shortcuts
{
    public partial class Form1 : Form
    {               
        public Form1()
        {
        InitializeComponent();
        }

        public string FileLocationToRun;
        DataSet ds = new DataSet("FileLocation");
        DataTable dt = new DataTable("Shortcut");

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog fdlg = new OpenFileDialog();
            fdlg.Title = "Browse";
            //fdlg.InitialDirectory = System.Environment.GetFolderPath(Environment.SpecialFolder.Recent);
            fdlg.Filter = "All Files.* (*.*)|*.*";
            fdlg.FilterIndex = 2;
            fdlg.RestoreDirectory = true;
            string fullpath = "";
            if (fdlg.ShowDialog() == DialogResult.OK)
            {
                fullpath = System.IO.Path.GetFullPath(fdlg.FileName);
                textBox1.Text = fullpath;
            }
        }

        string xsd = Application.StartupPath + "\\xmlfile.xsd";
        string xml = Application.StartupPath + "\\xmlfile.xml";
        private void button2_Click_1(object sender, EventArgs e)
        {
            if (textBox1.Text.Equals("") | textBox2.Text.Equals(""))
            {
                MessageBox.Show("One of the required fields does not contain any text", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            
            else
            {
                //if (!File.Exists(xsd))
                //{
                //    //dataGridView1.Rows.Add(textBox2.Text, textBox1.Text);
                //    ds.Tables.Add(dt);
                //    dt.Columns.Add("FileName");
                //    dt.Columns.Add("FileLocation");
                //    ds.WriteXmlSchema(xsd);
                //    ds.WriteXml(xml);
                //    //ds.Tables["Shortcut"].Rows.Add(textBox2.Text, textBox1.Text);
                //}
                //else
                //{
                //    ds.ReadXmlSchema(xsd);
                //    ds.ReadXml(xml);
                //}


            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                FileLocationToRun = dataGridView1.SelectedCells[1].Value.ToString();
                Process.Start(FileLocationToRun);
            }
            catch
            {
                MessageBox.Show("Your file location provided is a broken link");
            }
        }
        
        private void button4_Click(object sender, EventArgs e)
        {


        }

        private void button5_Click(object sender, EventArgs e)
        {

        }

       

        
    }
}

form1.designer.cs

namespace Shortcuts
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.button1 = new System.Windows.Forms.Button();
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.textBox2 = new System.Windows.Forms.TextBox();
            this.listBox1 = new System.Windows.Forms.ListBox();
            this.label1 = new System.Windows.Forms.Label();
            this.button2 = new System.Windows.Forms.Button();
            this.label2 = new System.Windows.Forms.Label();
            this.dataGridView1 = new System.Windows.Forms.DataGridView();
            this.Filename = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.FileLocation = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.button3 = new System.Windows.Forms.Button();
            this.label3 = new System.Windows.Forms.Label();
            this.label4 = new System.Windows.Forms.Label();
            this.button4 = new System.Windows.Forms.Button();
            this.button5 = new System.Windows.Forms.Button();
            ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
            this.SuspendLayout();
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(356, 386);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(76, 20);
            this.button1.TabIndex = 1;
            this.button1.Text = "Browse";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // textBox1
            // 
            this.textBox1.Location = new System.Drawing.Point(113, 386);
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(237, 20);
            this.textBox1.TabIndex = 2;
            // 
            // textBox2
            // 
            this.textBox2.Location = new System.Drawing.Point(113, 412);
            this.textBox2.Name = "textBox2";
            this.textBox2.Size = new System.Drawing.Size(237, 20);
            this.textBox2.TabIndex = 3;
            // 
            // listBox1
            // 
            this.listBox1.ColumnWidth = 184;
            this.listBox1.FormattingEnabled = true;
            this.listBox1.Location = new System.Drawing.Point(11, 12);
            this.listBox1.MultiColumn = true;
            this.listBox1.Name = "listBox1";
            this.listBox1.Size = new System.Drawing.Size(421, 368);
            this.listBox1.TabIndex = 5;
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(11, 389);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(70, 13);
            this.label1.TabIndex = 6;
            this.label1.Text = "File Location:";
            // 
            // button2
            // 
            this.button2.BackgroundImageLayout = System.Windows.Forms.ImageLayout.None;
            this.button2.Location = new System.Drawing.Point(356, 412);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(76, 20);
            this.button2.TabIndex = 7;
            this.button2.Text = "Add";
            this.button2.UseVisualStyleBackColor = true;
            this.button2.Click += new System.EventHandler(this.button2_Click_1);
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(11, 416);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(91, 13);
            this.label2.TabIndex = 8;
            this.label2.Text = "Name of shortcut:";
            // 
            // dataGridView1
            // 
            this.dataGridView1.AllowUserToAddRows = false;
            this.dataGridView1.AllowUserToDeleteRows = false;
            this.dataGridView1.AllowUserToResizeRows = false;
            this.dataGridView1.BackgroundColor = System.Drawing.SystemColors.ButtonHighlight;
            this.dataGridView1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
            this.Filename,
            this.FileLocation});
            this.dataGridView1.GridColor = System.Drawing.SystemColors.ButtonHighlight;
            this.dataGridView1.Location = new System.Drawing.Point(11, 12);
            this.dataGridView1.MultiSelect = false;
            this.dataGridView1.Name = "dataGridView1";
            this.dataGridView1.ReadOnly = true;
            this.dataGridView1.RowHeadersBorderStyle = System.Windows.Forms.DataGridViewHeaderBorderStyle.Single;
            this.dataGridView1.RowHeadersVisible = false;
            this.dataGridView1.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
            this.dataGridView1.Size = new System.Drawing.Size(421, 368);
            this.dataGridView1.TabIndex = 9;
            // 
            // Filename
            // 
            this.Filename.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.Fill;
            this.Filename.HeaderText = "FileName";
            this.Filename.Name = "Filename";
            this.Filename.ReadOnly = true;
            // 
            // FileLocation
            // 
            this.FileLocation.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.Fill;
            this.FileLocation.HeaderText = "FileLocation";
            this.FileLocation.Name = "FileLocation";
            this.FileLocation.ReadOnly = true;
            // 
            // button3
            // 
            this.button3.BackgroundImageLayout = System.Windows.Forms.ImageLayout.None;
            this.button3.Location = new System.Drawing.Point(11, 438);
            this.button3.Name = "button3";
            this.button3.Size = new System.Drawing.Size(421, 30);
            this.button3.TabIndex = 10;
            this.button3.Text = "Load";
            this.button3.UseVisualStyleBackColor = true;
            this.button3.Click += new System.EventHandler(this.button3_Click);
            // 
            // label3
            // 
            this.label3.AutoSize = true;
            this.label3.BackColor = System.Drawing.Color.Transparent;
            this.label3.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label3.ForeColor = System.Drawing.Color.Red;
            this.label3.Location = new System.Drawing.Point(76, 386);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(13, 16);
            this.label3.TabIndex = 11;
            this.label3.Text = "*";
            // 
            // label4
            // 
            this.label4.AutoSize = true;
            this.label4.BackColor = System.Drawing.Color.Transparent;
            this.label4.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label4.ForeColor = System.Drawing.Color.Red;
            this.label4.Location = new System.Drawing.Point(97, 413);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(13, 16);
            this.label4.TabIndex = 12;
            this.label4.Text = "*";
            // 
            // button4
            // 
            this.button4.BackgroundImageLayout = System.Windows.Forms.ImageLayout.None;
            this.button4.Location = new System.Drawing.Point(11, 474);
            this.button4.Name = "button4";
            this.button4.Size = new System.Drawing.Size(209, 30);
            this.button4.TabIndex = 13;
            this.button4.Text = "Save";
            this.button4.UseVisualStyleBackColor = true;
            this.button4.Click += new System.EventHandler(this.button4_Click);
            // 
            // button5
            // 
            this.button5.BackgroundImageLayout = System.Windows.Forms.ImageLayout.None;
            this.button5.Location = new System.Drawing.Point(223, 474);
            this.button5.Name = "button5";
            this.button5.Size = new System.Drawing.Size(209, 30);
            this.button5.TabIndex = 14;
            this.button5.Text = "Refresh";
            this.button5.UseVisualStyleBackColor = true;
            this.button5.Click += new System.EventHandler(this.button5_Click);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(446, 517);
            this.Controls.Add(this.button5);
            this.Controls.Add(this.button4);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.button3);
            this.Controls.Add(this.dataGridView1);
            this.Controls.Add(this.button2);
            this.Controls.Add(this.listBox1);
            this.Controls.Add(this.textBox2);
            this.Controls.Add(this.textBox1);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.label4);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.TextBox textBox1;
        private System.Windows.Forms.TextBox textBox2;
        private System.Windows.Forms.ListBox listBox1;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Button button2;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.DataGridView dataGridView1;
        private System.Windows.Forms.Button button3;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.Label label4;
        private System.Windows.Forms.Button button4;
        private System.Windows.Forms.Button button5;
        private System.Windows.Forms.DataGridViewTextBoxColumn Filename;
        private System.Windows.Forms.DataGridViewTextBoxColumn FileLocation;
    }
}

xmlfile.xml (its set as a resource)

<?xml version="1.0" encoding="utf-8" ?>
<Table>
  
  <Shortcut>
    <FileLocation>c:/</FileLocation>
    <FileName>lol</FileName>
  </Shortcut>
  
  <Shortcut>
    <FileLocation>c:/</FileLocation>
    <FileName>hehe</FileName>
  </Shortcut>
  
</Table>

xmlfile.xsd (also set as a resource)

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="xmlfile"
    targetNamespace="http://tempuri.org/xmlfile.xsd"
    elementFormDefault="qualified"
    xmlns="http://tempuri.org/xmlfile.xsd"
    xmlns:mstns="http://tempuri.org/xmlfile.xsd"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
</xs:schema>

You still didnt show me where it come to the error.
Take a look at this simple example of to populate dgw from xml file:

private void Form1_Load(object sender, EventArgs e)
 {
           XmlDataDocument xmlDatadoc = new XmlDataDocument();
            xmlDatadoc.DataSet.ReadXml("C:\\books.xml");
            DataSet ds = new DataSet("Books DataSet");
            ds = xmlDatadoc.DataSet;
            dataGridView1.DataSource = ds.DefaultViewManager;
            dataGridView1.DataMember = "Book";
  }
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Xml;
using System.IO;
using System.Xml.Linq;

namespace Shortcuts
{
    public partial class Form1 : Form
    {               
        public Form1()
        {
        InitializeComponent();
        }

        public string FileLocationToRun;

        private void Form1_Load(object sender, EventArgs e)
        {
            XmlDataDocument xmlDatadoc = new XmlDataDocument();
            xmlDatadoc.DataSet.ReadXml("C:\\xmlfile.xml");
            DataSet ds = new DataSet("Books DataSet");
            ds = xmlDatadoc.DataSet;
            dataGridView1.DataSource = ds.DefaultViewManager;
            dataGridView1.DataMember = "Shortcut";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog fdlg = new OpenFileDialog();
            fdlg.Title = "Browse";
            //fdlg.InitialDirectory = System.Environment.GetFolderPath(Environment.SpecialFolder.Recent);
            fdlg.Filter = "All Files.* (*.*)|*.*";
            fdlg.FilterIndex = 2;
            fdlg.RestoreDirectory = true;
            string fullpath = "";
            if (fdlg.ShowDialog() == DialogResult.OK)
            {
                fullpath = System.IO.Path.GetFullPath(fdlg.FileName);
                textBox1.Text = fullpath;
            }
        }

        private void button2_Click_1(object sender, EventArgs e)
        {
            if (textBox1.Text.Equals("") | textBox2.Text.Equals(""))
            {
                MessageBox.Show("One of the required fields does not contain any text", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            else
            {
                XmlDataDocument xmlDatadoc = new XmlDataDocument();
                xmlDatadoc.DataSet.ReadXml("C:\\xmlfile.xml");
                DataSet ds = new DataSet("Books DataSet");
                ds = xmlDatadoc.DataSet;
                dataGridView1.DataSource = ds.DefaultViewManager;
                dataGridView1.DataMember = "Shortcut";
            }
        }


        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                FileLocationToRun = dataGridView1.SelectedCells[1].Value.ToString();
                Process.Start(FileLocationToRun);
            }
            catch
            {
                MessageBox.Show("Your file location provided is a broken link");
            }
        }
        
        private void button4_Click(object sender, EventArgs e)
        {


        }

        private void button5_Click(object sender, EventArgs e)
        {

        }

       

        
    }
}

ive got it to read the xml file but it adds 2 more columns to make 4 columns =[

I now need to know how to export the xml file

p.s. do you have aim ?

You still didnt show me where it come to the error.
Take a look at this simple example of to populate dgw from xml file:

private void Form1_Load(object sender, EventArgs e)
 {
           XmlDataDocument xmlDatadoc = new XmlDataDocument();
            xmlDatadoc.DataSet.ReadXml("C:\\books.xml");
            DataSet ds = new DataSet("Books DataSet");
            ds = xmlDatadoc.DataSet;
            dataGridView1.DataSource = ds.DefaultViewManager;
            dataGridView1.DataMember = "Book";
  }

how do i create an xml file from scratch ?

You still didnt show me where it come to the error.
Take a look at this simple example of to populate dgw from xml file:

private void Form1_Load(object sender, EventArgs e)
 {
           XmlDataDocument xmlDatadoc = new XmlDataDocument();
            xmlDatadoc.DataSet.ReadXml("C:\\books.xml");
            DataSet ds = new DataSet("Books DataSet");
            ds = xmlDatadoc.DataSet;
            dataGridView1.DataSource = ds.DefaultViewManager;
            dataGridView1.DataMember = "Book";
  }

actually before i get too ahead of myself just yet:

dataGridView1.Rows.Add(textBox2.Text, textBox1.Text);

it says

Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound.

What do you mean 4 columns? 2 on the form_load event and 2 on button click?

Look, this way we are getting no where. 1st of all would be good that you study some literature, that you will get the basics covered.
You cant just do import of the xml file, not knowing what are you importing and where. I will give you now some points how you need to approach to this project:
- create dgv (with all the columns)
- fill dgv with some data
- do the export to xml
- finaly: only now you can do import of the pre-exported xml file back to dgv.

You got now the whole picture. Please don`t jump things, cause we wont get any further. I am trying to help you out here, but your knowledge is apperently not on the level to cope with this kinda task.
So i really suggest you, please learn some basics, start with more easier things, if this is too much for you atm. One step at a time.
Programming is a huge library of knowledge, and you wont get it over night. Especially you wont learn it, if I will do all the work.

So, PLEASE if you want me to help you (or someone else here) out with this project, go by the points I`ve stated up there. Step by step. And if there will be any problem let me know (create a thread).
And please close this one (mark as answered), becuase I have answered you on too many questions already. Threads are meant to stay on a threads topics.

no hard feelings, i know how this is, I was in the same position a while back,
best regards,

Mitja

no its not that, its just that i get confused really easily and things need explaining in my eyes. If i understand how the code works i will be able to do this.

look at programs i have made:

otrippinz.com

that is my capability.

This is the first time in my life i have dealt with an XML file =/

Xml is nothing harder to cope with, then a simple txt file, only that its screpted.
Please ust go over the lines that I have created in the previous post, and you will see how easy can all be - the most importatn in coding is approach. Take slow steps forward, not all at ones.
Read some literature of how to use xml, google knows all.
But if you still insist, I will do a particual example onl for you - exporting to xml and importing them back to dgv.
ok?

Xml is nothing harder to cope with, then a simple txt file, only that its screpted.
Please ust go over the lines that I have created in the previous post, and you will see how easy can all be - the most importatn in coding is approach. Take slow steps forward, not all at ones.
Read some literature of how to use xml, google knows all.
But if you still insist, I will do a particual example onl for you - exporting to xml and importing them back to dgv.
ok?

oh thanks !

when you do the example can you please use multiple columns ?

this should help me alot !

But if you still insist, I will do a particual example onl for you - exporting to xml and importing them back to dgv.
ok?

any luck ?

Yes, close to the finish. I only have to do the import.

thanks =]

Link to the project is HERE.
I hope this is what you hae been looking for.

cya

Link to the project is HERE.
I hope this is what you hae been looking for.

cya

thanks,

Let me know if is was in any help. If not (completely), please tell me what is needed to repair, ok?
cya
Mitja

No its all good !

Thanks, this explains alot !

Hi

This code is very helpful in our project. thank you to give this code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.