Updating Datagrid Row To Save To Sql
Hi Guys I am trying to understand how to save and edited row to the database private void BudgetGrid_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e) { SqlCom
Solution 1:
Just copy below codes. I've created all the thing of you and tested successfully. Rather than the first way, I tried to let you go more popular way. Therefore, it took me time to adopt..
Hope this helps you !
SqlDataAdapter da;
DataTable dt;
privatevoidWindow_Loaded(object sender, RoutedEventArgs e)
{
SqlConnectionConn=newSqlConnection();
Conn.ConnectionString = yourConnectionString;
Conn.Open();
SqlCommandgridcomm=newSqlCommand();
gridcomm.Connection = Conn;
gridcomm.CommandText = "SELECT Id, Name, Quantity, Rate, Time FROM Budget";
da = newSqlDataAdapter(gridcomm);
SqlDataReadergridreader= gridcomm.ExecuteReader();
while (gridreader.Read())
{
}
gridreader.Close();
dt= newDataTable("Budget");
da.Fill(dt);
dataGrid_Budget.ItemsSource = dt.DefaultView;
Conn.Close();
}
privatevoiddataGrid_Budget_RowEditEnding(object sender, System.Windows.Controls.DataGridRowEditEndingEventArgs e)
{
DataGridRoweditedrow= e.Row;
introw_index= (DataGrid)sender).ItemContainerGenerator.IndexFromContainer(editedrow);
for (int k=0;k< 5;k++)
{
DataGridCellcell= GetCell(row_index, k);
TextBlocktb= cell.Content as TextBlock;
if (k==1)
{
dt.Rows[row_index][k] = tb.Text;
}
elseif (k == 4)
{
if (tb.Text != "")
{
dt.Rows[row_index][k] = Convert.ToDateTime(tb.Text);
}
}
else
{
dt.Rows[row_index][k] = Convert.ToInt32(tb.Text);
}
}
da.UpdateCommand = newSqlCommandBuilder(da).GetUpdateCommand();
da.Update(dt);
}
public DataGridCell GetCell(int row, int column)
{
DataGridRowrowContainer= GetRow(row);
if (rowContainer != null)
{
DataGridCellsPresenterpresenter= GetVisualChild<DataGridCellsPresenter>(rowContainer);
DataGridCellcell= (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
if (cell == null)
{
dataGrid_Budget.ScrollIntoView(rowContainer, dataGrid_Budget.Columns[column]);
cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
}
return cell;
}
returnnull;
}
public DataGridRow GetRow(int index)
{
DataGridRowrow= (DataGridRow)dataGrid_Budget.ItemContainerGenerator.ContainerFromIndex(index);
if (row == null)
{
dataGrid_Budget.UpdateLayout();
dataGrid_Budget.ScrollIntoView(dataGrid_Budget.Items[index]);
row = (DataGridRow)dataGrid_Budget.ItemContainerGenerator.ContainerFromIndex(index);
}
return row;
}
publicstatic T GetVisualChild<T>(Visual parent) where T : Visual
{
Tchild=default(T);
intnumVisuals= VisualTreeHelper.GetChildrenCount(parent);
for (inti=0; i < numVisuals; i++)
{
Visualv= (Visual)VisualTreeHelper.GetChild(parent, i);
child = v as T;
if (child == null)
{
child = GetVisualChild<T>(v);
}
if (child != null)
{
break;
}
}
return child;
}
Solution 2:
Your SQL syntax has to be corrected like,
SqlCommandupdate_comm=newSqlCommand();
update_comm.Connection = Conn;
update_comm.CommandText = "UPDATE Budget SET id= @u_id, Name= @u_name WHERE person= @psn";
varupdate_da=newSqlDataAdapter(update_comm);
update_da.SelectCommand.Parameters.Add(newSqlParameter("@u_id", SqlDbType.Int));
update_da.SelectCommand.Parameters["@u_id"].Value = yourintvalue;
update_da.SelectCommand.Parameters.Add(newSqlParameter("@u_name", SqlDbType.NVarChar));
update_da.SelectCommand.Parameters["@u_name"].Value = yourstringvalue;
update_da.SelectCommand.Parameters.Add(newSqlParameter("@psn", SqlDbType.NVarChar));
update_da.SelectCommand.Parameters["@psn"].Value = yourstringvalue;
varupdate_ds=newDataSet();
update_da.Fill(update_ds);
'UPDATE' should be used with 'SET' together.
And if you want to update the actual SQL database with the value of edited rows of DataGrid, please try this.
da.UpdateCommand = newSqlCommandBuilder(da).GetUpdateCommand();
da.Update(griddt);
Solution 3:
SqlConnectionuniConn=null;
SqlCommandcmd=null;
SqlDataAdaptersda=null;
DataTabledt=newDataTable();
uniConn = newSqlConnection(
"server=localhost;" + "Trusted_Connection=yes;" +
"database=Production; " + "connection timeout=30");
cmd = newSqlCommand("UPDATE Budget(id, Name, Quantity, Rate, Time)",
uniConn);
uniConn.Open();
sda = newSqlDataAdapter(cmd);
sda.Fill(dt);
BudgetGrid.ItemsSource = dt.DefaultView;
uniConn.Close();
Did you forget to close the connection?
Post a Comment for "Updating Datagrid Row To Save To Sql"