No1Coder
asked on
How to loop through a windows forms datagrid
I have a System.Windows.Forms.Datag rid that in want to loop though to update the data.
private System.Windows.Forms.DataG rid dataGridUrl;
The help system says there is an "Items" property, but when I try and access it I get a compile error "'System.Windows.Forms.Dat aGrid' does not contain a definition for 'Item' ...."
I am trying to:
foreach( (DataGridItem item in dataGridUrl.Item)
{
}
What am I missing?
private System.Windows.Forms.DataG
The help system says there is an "Items" property, but when I try and access it I get a compile error "'System.Windows.Forms.Dat
I am trying to:
foreach( (DataGridItem item in dataGridUrl.Item)
{
}
What am I missing?
one easy way is to loop through the datasource of the grid:
private void PrintCellValues(DataGrid myGrid){
int iRow;
int iCol;
DataTable myTable;
// Assumes the DataGrid is bound to a DataTable.
myTable = (DataTable) dataGrid1.DataSource;
for(iRow = 0;iRow < myTable.Rows.Count ;iRow++) {
for(iCol = 0;iCol < myTable.Columns.Count ;iCol++) {
Console.WriteLine(myGrid[iRow, iCol]);
}
}
}
or loop through the rows of the grid:
foreach (DataGridViewRow row in dgvDataGridView.Rows)
{
(row.Cells[0]).Value = "something";
}
ASKER
If I type in data in one of the fields in the grid, the loop through, the data is not updated. I get the original value. How do I "accept changes"?
ASKER
Also, one of the columns in the grid is a checkbox. How do I read the state of the checkbox?
object col1 = dataGridUrl[0][1]; // this is a checkbox column
What is the object?
object col1 = dataGridUrl[0][1]; // this is a checkbox column
What is the object?
Strange. Have you tried both methods? Can you show your code?
have you tried something like:
foreach (DataGridViewRow row in dgvDataGridView.Rows)
{
((DataGridViewCheckBoxCell)row.Cells[0]).Value = true;
}
ASKER
I am simply trying to update the underlying table that the data grid is bound to. One would thing there would be a standard method for doing this, but I can't find it.
I am using a DataGrid. Not a DataGridView.
DataTable urls = ds["urls"]; // get table from data set
int iRow, iCol;
for (iRow = 0; iRow < urls.Rows.Count; iRow++)
{
object o = dataGridUrl[iRow, 0]; // this gets the original string value, not the value I type into the grid
object o1 = dataGridUrl[iRow, 1]; // this returns an object but I don't know what it is. Checkbox column
urls.Rows[iRow]["url"] = o;
urls.Rows[iRow]["disable"] = o1;
}
I am using a DataGrid. Not a DataGridView.
DataTable urls = ds["urls"]; // get table from data set
int iRow, iCol;
for (iRow = 0; iRow < urls.Rows.Count; iRow++)
{
object o = dataGridUrl[iRow, 0]; // this gets the original string value, not the value I type into the grid
object o1 = dataGridUrl[iRow, 1]; // this returns an object but I don't know what it is. Checkbox column
urls.Rows[iRow]["url"] = o;
urls.Rows[iRow]["disable"]
}
I just created a sample and it is working for me:
private DataTable _dt;
private void Form1_Load(object sender, EventArgs e)
{
_dt = new DataTable("test");
_dt.Columns.Add(new DataColumn("ID", typeof(Int32)));
_dt.Columns.Add(new DataColumn("Name", typeof (string)));
_dt.Columns.Add(new DataColumn("Selected", typeof(bool)));
dataGrid1.DataSource = _dt;
}
private void button1_Click(object sender, EventArgs e)
{
foreach (DataRow row in _dt.Rows)
{
int id = Convert.ToInt32(row["ID"].ToString());
string name = row["Name"].ToString();
bool selected = Convert.ToBoolean(row["Selected"].ToString());
int i = 0;
}
}
ASKER
Your code is reading from a table, not the grid. I am trying to update a table from a grid, where the user types in information. It doesn't appear to happen automatically. If I just enter data in a grid field, and don't click anything on the grid, and then I save the underlying table from a button click, it does not have the updated data. That is why I am trying to loop through the grid, so I can manually update the table.
>>Your code is reading from a table, not the grid
Try it. The grid is bound to the datatable so the content of the datatable is in sync with the grid
Try it. The grid is bound to the datatable so the content of the datatable is in sync with the grid
ASKER
Will you explain the theory behind this code? It doesn't make any sense to me.
Looks to me like it is just reading fields from the table.
Looks to me like it is just reading fields from the table.
the content of the grid is bound to the datatable. it is not a copy, it is a reference. so if you modify the datatable and/or the datagrid, the other one should be updated.
ASKER
Okay, but...
I enter data into a field on the grid, and click an update link. The update saves the dataset to an xml file. The data entered into the field is not saved. The original data is saved.
I enter data into a field on the grid, and click an update link. The update saves the dataset to an xml file. The data entered into the field is not saved. The original data is saved.
Have you tried my sample? When you click the button, it shows the new value you entered!
ASKER
Will try tomorrow but i dont understany why reading the datatable will fix my issue.
ASKER
I tried and it didn't work.
The grid has an entry field. I type in data, hit my save button, and the screen reverts back to the original value.
If I make a change to the field, and then add another row, and then click save, it does save the value.
I think something happens when the row changes to "accept the text".
The grid has an entry field. I type in data, hit my save button, and the screen reverts back to the original value.
If I make a change to the field, and then add another row, and then click save, it does save the value.
I think something happens when the row changes to "accept the text".
does your table have a primary key?
can you provide a sample?
can you provide a sample?
ASKER
No, it doesn't. It is not a sql table. Just a manually created table that gets stored as an xml file.
I discovered something about the grid. When I type data in a cell, the row indicator changed from a "carat" character, to a "pencil" character. If after typing in data, I click the Pencil icon, it saves the data to the data table.
I guess that is the way it works, but doesn't see very intuitive for users. Can I programmically switch from edit mode back to normal, and thereby cause the entry to be accepted.
I discovered something about the grid. When I type data in a cell, the row indicator changed from a "carat" character, to a "pencil" character. If after typing in data, I click the Pencil icon, it saves the data to the data table.
I guess that is the way it works, but doesn't see very intuitive for users. Can I programmically switch from edit mode back to normal, and thereby cause the entry to be accepted.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks
Open in new window