Reputation: 39
I'm building a small C# app, for myself and I'm not that much into programming (beginner) and I'm asking for your help. I am using a DataGridView
, which is read only, to display some records from a database
(MSSQL), which works fine. I would like to update the database without making the DataGridView
editable or by editing values through textboxes (all I could find so far). Is there any possibility to update the database
based on the selected row? Thanks.
Upvotes: 1
Views: 1232
Reputation: 952
My case is same that DataGrid is readonly, not editable and utilizing the selected values through below way,
This is not WinForm code but WPF code and you might follow the idea. Why don't you use WPF than WinForm?
Hope this helps..
private void button_Click(object sender, RoutedEventArgs e)
{
if (yourdatagrid.SelectedItem != null)
{
object item = yourdatagrid.SelectedItem;
string record= (yourdatagrid.SelectedCells[0].Column.GetCellContent(item) as TextBlock).Text;
// your SQL Update code with selected values here
// and you need to re-load values from database to DataGrid to be shown updated
}
}
Upvotes: 1