Click here to Skip to main content
15,884,177 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
HI Folks,
I am working in data grid view and I am assigning the datasource value from List of tuple collection. While binding I am not having any problem.
Please find below code
C#
List<tuple><bool,>> list = new List<tuple><bool,>>();
            list.Add(new Tuple<bool,>(true, "IPS"));
            list.Add(new Tuple<bool,>(false, "IAS"));
            list.Add(new Tuple<bool,>(true, "IRS"));

            dataGridView1.DataSource = list;
            dataGridView1.Visible = true;
            this.dataGridView1.Columns[0].HeaderText = "Status";
            this.dataGridView1.Columns[1].HeaderText = "Name";

            foreach (DataGridViewRow item in dataGridView1.Rows)
            {
                item.Cells["item1"].ReadOnly = false;
                item.Cells["item2"].ReadOnly = false;
            }

Once I saw the output in screen. Not I want to change the text from IPS to someother and I want to save in db. I am struggling at here. Can you please help on this. I need this result asap.
Appreciated your help

Thanks
Aravind Govindaraj
Posted
Updated 30-Apr-12 9:42am
v2
Comments
Shahin Khorshidnia 30-Apr-12 15:43pm    
After typing your code, Please select it, click on code and choose the language (c# for example)

1 solution

It is written in the question that the data is displayed in DataGridView. But I think this statement
C#
List<tuple><bool,>> list = new List<tuple><bool,>>();

may not be compiled, as no type argument is provided after ,.

Tuple is provided to create a data structure with required number of members. This is an immutable type, which means after creation of the Tuple it's member values cannot be changed. Hence, Tuple is not suitable for the above purpose.

For displaying the data, editing the data and saving the data to DB, a database is required. MS SQL Server, MS Access etc. can be used, for which first a database has to be created and the corresponding DataSet and DataTables are to be created in ADO.NET. There are other options available like Entity FrameWork, XML etc.

For this purpose I made a code sample using a DataTable which can be saved to an XML file and the data saved to the xml file can be read into the DataTable using the save and open buttons, as shown below
C#
DataTable dataTable1 = new DataTable();
dataTable1.TableName = "NameStatus";
dataTable1.Columns.Add("Status", typeof(bool), null);
dataTable1.Columns.Add("Name", typeof(string), null);

dataTable1.Rows.Add(true, "IPS");
dataTable1.Rows.Add(false, "IAS");
dataTable1.Rows.Add(true, "IRS");

DataGridView dataGridView1 = new DataGridView();
dataGridView1.DataSource = dataTable1;
dataGridView1.Dock = DockStyle.Fill;
Controls.Add(dataGridView1);


Button openButton = new Button();
openButton.Dock = DockStyle.Bottom;
openButton.Text = "Open";
Controls.Add(openButton);
openButton.Click += (s, args) => {
    OpenFileDialog openFileDialog = new OpenFileDialog();
    openFileDialog.Filter = "XML file | *.xml";
    openFileDialog.ShowDialog();
    dataTable1.Clear();
    dataTable1.ReadXml(openFileDialog.FileName);
    openFileDialog.Dispose();
};

Button saveButton = new Button();
saveButton.Dock = DockStyle.Bottom;
saveButton.Text = "Save";
Controls.Add(saveButton);
saveButton.Click += (s, args) => {
    SaveFileDialog saveFileDailog = new SaveFileDialog();
    saveFileDailog.Filter = "XML file | *.xml";
    saveFileDailog.ShowDialog();
    dataTable1.WriteXml(saveFileDailog.FileName);
    saveFileDailog.Dispose();
};

To run the above sample, create Windows Forms application double click on Form1 in the designer to create method body for the the Load event of Form1, paste above code in the Form1_Load method and run the application.
 
Share this answer
 
v2
Comments
[no name] 30-Apr-12 21:58pm    
Above and beyond as far as I am concerned.
VJ Reddy 30-Apr-12 23:01pm    
Thank you, Wes.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900