Datagridview1 contains 3 columns.
1st : ID
2nd : Name
3rd : ModifiedDate
third column is hiden
I would like to update automatically the hidden column (Modified Date) each time I modify the Second column value (Name)
Something is wrong with the next :
dataGridView1.CurrentRow.Cells(2).Value = dateTime.Now;
MyEntities.SaveChanges();
Thank you in advance.
Loading
VulpesPosted May 14, 2012, 10:38 AM
VulpesPosted May 14, 2012, 11:15 AM
So, I'd use:
SamioPosted May 14, 2012, 10:47 AM
When tried you proposal, I got e.RowIndex = -1
Then used: if (e.RowIndex > -1) instead of (e.ColumnIndex == 1) and it worked
I need your help for this please
SamioPosted May 14, 2012, 9:33 AM
{
dataGridView1.CurrentRow.Cells[3].Value = DateTime.Now;
}
I get error, and it looks like the current row is null
EhteshamPosted May 14, 2012, 7:07 AM
SamioPosted May 14, 2012, 7:01 AM
I Think we are near of the solution.
All what is needed here is just to update one datagrid cell when another just has been changed. Do you know the right event to use?
Many thanks
EhteshamPosted May 14, 2012, 6:43 AM
SamioPosted May 14, 2012, 6:24 AM
also it is not good practice to select the whole row each time you need to update the hidden column, I think it should be better to update it once the current cell value changed.
EhteshamPosted May 14, 2012, 5:23 AM
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
DataTable dt = new DataTable();
dt.Columns.Add("Col1", typeof(string));
dt.Columns.Add("Col2", typeof(string));
dt.Columns.Add("Col3", typeof(string));
DataRow dr = dt.NewRow();
dr["Col1"] = "ValRow1Col1";
dr["Col2"] = "ValRow1Col2";
dr["Col3"] = "ValRow1Col2";
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["Col1"] = "ValRow2Col1";
dr["Col2"] = "ValRow2Col2";
dr["Col3"] = "ValRow2Col2";
dt.Rows.Add(dr);
dataGridView1.DataSource = dt;
this.dataGridView1.Columns["Col3"].Visible = false;
}
private void button1_Click(object sender, EventArgs e)
{
this.dataGridView1.CurrentRow.Cells["Col3"].Value = "Changed";
MessageBox.Show(this.dataGridView1.CurrentRow.Cells["Col3"].Value.ToString());
}
}
Make sure you have selected row from datagridview
SamioPosted May 14, 2012, 5:10 AM
dataGridView1.CurrentRow.Cells[2].Value = DateTime.Now
instead of
dataGridView1.CurrentRow.Cells(2).Value = DateTime.Now
solved the compile error.
Please have you an idea on how to change the hidden column value of the current row by code ? I mean which event of datagridview should be used to allow saving values in several rows before persisting data to the database.
EhteshamPosted May 14, 2012, 4:43 AM
I assume you are getting compile time error as you are using c# not vb.net
SamioPosted May 14, 2012, 4:03 AM
Could not use dt.GetChanges, my dataGridView1.datasource is bindingSource1 and bindingSource1.DataSource = nw.Orders
Posted May 14, 2012, 2:15 AM
To fetch the modified records,
DataTable changedDatatable = dt.GetChanges();
Once the you got the modified records, then you can call MyEntities.SaveChanges();
Please let me know, your thoughts on this.