Change Datarow field value
Asked Answered
C

3

12

First I have last update file from DB

DataTable excelData = ReadSCOOmega(lastUploadFile);

, after this iterate over this data

foreach (DataRow currentRow in rows)
{
     currentRow.
}

Is that possible to change da data in the foreach loop.I can access only the value of this data

currentRow.Field<object>("Some column name")

but not to change it.My idea is selected.I have a multiple deals in excel file and when is upload to DB, I need to make changes in this file.Is that possible or I need to store the data in other collection?

Clostridium answered 10/5, 2016 at 17:9 Comment(0)
P
16

You can use indexer to set the data stored to the field: currentRow["columnName"] = value.

See MSDN DataRow.Item Property

Poland answered 10/5, 2016 at 17:18 Comment(0)
S
21

You can do that like :

foreach (DataRow currentRow in excelData.Rows)
{
    currentRow.BeginEdit();
    currentRow["ColumnName"] = value;
    //.....
    currentRow.EndEdit();
}
Spriggs answered 10/5, 2016 at 19:46 Comment(0)
P
16

You can use indexer to set the data stored to the field: currentRow["columnName"] = value.

See MSDN DataRow.Item Property

Poland answered 10/5, 2016 at 17:18 Comment(0)
W
0

some useful code

dgSurvey.Columns["update"].Visible = true;

//update row

int ActualRowindex = (currentpage - 1) * pagesize + rowindex;
DataGridViewRow drow = dgSurvey.Rows[rowindex];
Surveydt.Rows[ActualRowindex]["person"] = drow.Cells["person"].Value;

//delete row
  Surveydt.Rows[ActualRowindex].Delete();

//search

foreach(DataRow drow in Surveydt.Rows)
{
    foreach(object cell in drow.ItemArray)
    {
        if (cell.ToString().ToLower().Contains(searchtext.ToString().ToLower()))
        {
            searchdt.ImportRow(drow);
            break;
        }
    }

}

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    if (e.ColumnIndex == dataGridView1.Columns["Action"].Index && e.RowIndex >= 0)
    {
        MessageBox.Show("Button clicked in row " + e.RowIndex);
    }
}


DataGridViewButtonColumn buttonColumn = new DataGridViewButtonColumn();
// Set the properties of the column
buttonColumn.Name = "MyButtonColumn";
buttonColumn.Text = "Click Me";
buttonColumn.UseColumnTextForButtonValue = true;
// Add the column to the DataGridView
dataGridView1.Columns.Add(buttonColumn);
Whitford answered 23/8, 2024 at 1:36 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.