Reputation: 23
I'm working on a C# application and using the DataGridView component. My datagrid is connected to my database and loads a single data table. The user fills empty fields and saves the changes on the DataGridView and also database. The question is that I need to use a specific column's index that the user filled.
This is the code that creates the datagrid:
void gridDoldur()
{
try
{
string query = "SELECT DersId FROM Dersler WHERE DersAd= @dersad";
SqlConnection conn = new SqlConnection("Data Source=SEFA-PC;Initial Catalog=proje;Integrated Security=True;Pooling=False;MultipleActiveResultSets=True");
SqlCommand comd = new SqlCommand(query, conn);
comd.Parameters.AddWithValue("@dersad", dersBox.SelectedItem);
conn.Open();
SqlDataReader dre; // dersin adından id'si çekildi
dre = comd.ExecuteReader();
dre.Read();
int dersId = dre.GetInt32(0);
dt.Clear();
sda = new SqlDataAdapter("SELECT * FROM Notlar WHERE DersId=" + dersId, conn);
sda.Fill(dt);
notGrid.DataSource = dt;
notGrid.Visible = true;
kaydetButton.Visible = true;
notGrid.Columns[0].Visible = false;
notGrid.Columns[8].Visible = false;
notGrid.Columns[1].ReadOnly = true;
notGrid.Columns[7].ReadOnly = true;
sBuilder = new SqlCommandBuilder(sda);
conn.Close();
}
And this is how my grid looks: https://i.sstatic.net/rLagd.jpg
I need to access elements of edited rows.
Upvotes: 2
Views: 487
Reputation: 5454
This may be overkill for your purposes since it will trigger every time any cell in a row is changed, however you can get a general idea from it hopefully. You can easily grab the row index using the DataGridView's CellValueChanged event handler. The idea here would be to note which rows have had a change in this method, like so:
private List<int> editedRows = new List<int>;
private void myDGV_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if (!this.editedRows.Contains(e.RowIndex))
{
editedRows.Add(e.RowIndex);
}
}
With this idea, you could call your method something like this:
foreach (int row in this.editedRows)
{
this.MyMethod((int)myDGV[row, 0].Value, (int)myDGV[row, 1].Value, (int)myDGV[row, 2].Value, ...);
}
Or:
foreach (int row in this.editedRows)
{
DataGridViewRow r = myDGV.Rows[row];
this.MyMethod((int)r.Cells[0].Value, (int)r.Cells[1].Value, (int)r.Cells[2].Value, ...);
}
Note: Remember to cast the cell value to your data type (I assumed int based on the screenshot).
I did't see the context of your method anywhere, so hopefully this gives you a general idea of where to go next.
Edit: I would go with Jeff's answer. It gets the same idea across without the need of manually storing a list of updated rows.
Upvotes: 0
Reputation: 8982
You can spin over the rows on the DataTable and check their DataRowState to determine if they have changed.
foreach (DataRow row in dt.Rows)
{
if (row.RowState == DataRowState.Modified)
{
var infoINeed = (int) row["SomeInformation"];
var infoByColumnIndex = row[3].ToString();
// Do stuff
}
}
dt.AcceptChanges();
Once you are finished with your processing, you'll want to be sure to call AcceptChanges()
. This will change the RowState
on DataRowState.Modified
rows to DataRowState.Unchanged
.
Upvotes: 1