更新DATAGRID行以保存到SQL [英] updating DATAGRID row to save to SQL
问题描述
我好想知道如何保存和编辑行到db
private void BudgetGrid_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e)
{
SqlCommand gridcmd = new SqlCommand();
SqlConnection rwConn = null;
rwConn = new SqlConnection(server = localhost;+Trusted_Connection = yes;+database = Production;+connection timeout = 30);
gridcmd.Connection = rwConn;
rwConn.Open();
//gridcmd.CommandText =SELECT Id,Name,Quantity,Rate,Time FROM Budget;
gridcmd.CommandText =UPDATE Budget SET Id = @id,Name = @Name,Quantity = @Qty,Rate = @Rte WHERE Time = @Time;
SqlDataAdapter gridda = new SqlDataAdapter(gridcmd);
string strId =@id.ToString();
int intID;
bool bintID = Int32.TryParse(strId,out intID);
string strName =@Name.ToString();
string strQty =@Qty.ToString();
int intQty;
bool bintQty = Int32.TryParse(strQty,out intQty);
string strRte =@Rte.ToString();
int intRte;
bool bintRte = Int32.TryParse(strRte,out intRte);
string strTime =@Time.ToString();
gridda.SelectCommand.Parameters.Add(new SqlParameter(@ id,SqlDbType.Int));
gridda.SelectCommand.Parameters [@ id]。SqlValue = intID;
gridda.SelectCommand.Parameters.Add(新的SqlParameter(@ Name,SqlDbType.VarChar));
gridda.SelectCommand.Parameters [@ Name]。SqlValue = strName;
gridda.SelectCommand.Parameters.Add(new SqlParameter(@ Qty,SqlDbType.Int));
gridda.SelectCommand.Parameters [@ Qty]。SqlValue = strQty;
gridda.SelectCommand.Parameters.Add(new SqlParameter(@ Rte,SqlDbType.Int));
gridda.SelectCommand.Parameters [@ Rte]。SqlValue = strRte;
gridda.SelectCommand.Parameters.Add(new SqlParameter(@ Time,SqlDbType.VarChar));
gridda.SelectCommand.Parameters [@ Time]。SqlValue = strTime;
DataTable griddt = new DataTable(Budget);
gridda.Fill(griddt);
gridda.UpdateCommand = new SqlCommandBuilder(gridda).GetUpdateCommand();
BudgetGrid.ItemsSource = griddt.DefaultView;
gridda.Update(griddt);
rwConn.Close();
}
它显示正常。我可以编辑它,但是当我点击另一个选项卡时,它不会更新它可以返回到原始数据。
我的大部分代码都是通过它出现日期..或不是我要找的。 p>
所以这里是数据库
这里是应用程序
所以基本上如果我打标签到下一行。在事件 BudgetGrid_RowEditEnding
它应该更新数据库..但现在它不是。
只需复制下面的代码。我创建了所有的事情并成功测试。而不是第一种方式,我试图让你走更受欢迎的方式。因此,我花了我的时间来采用..
希望这可以帮助你!
SqlDataAdapter da;
DataTable dt;
private void Window_Loaded(object sender,RoutedEventArgs e)
{
SqlConnection Conn = new SqlConnection();
Conn.ConnectionString = yourConnectionString;
Conn.Open();
SqlCommand gridcomm = new SqlCommand();
gridcomm.Connection = Conn;
gridcomm.CommandText =SELECT Id,Name,Quantity,Rate,Time FROM Budget;
da = new SqlDataAdapter(gridcomm);
SqlDataReader gridreader = gridcomm.ExecuteReader();
while(gridreader.Read())
{
}
gridreader.Close();
dt = new DataTable(Budget);
da.Fill(dt);
dataGrid_Budget.ItemsSource = dt.DefaultView;
Conn.Close();
}
private void dataGrid_Budget_RowEditEnding(object sender,System.Windows.Controls.DataGridRowEditEndingEventArgs e)
{
DataGridRow editedrow = e.Row;
int row_index =(DataGrid)sender).ItemContainerGenerator.IndexFromContainer(editedrow);
for(int k = 0; k <5; k ++)
{
DataGridCell cell = GetCell(row_index,k);
TextBlock tb = cell.Content为TextBlock;
if(k == 1)
{
dt.Rows [row_index] [k] = tb.Text;
}
else if(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 = new SqlCommandBuilder(da).GetUpdateCommand();
da.Update(dt);
}
public DataGridCell GetCell(int row,int column)
{
DataGridRow rowContainer = GetRow );
if(rowContainer!= null)
{
DataGridCellsPresenter Presenter = GetVisualChild< DataGridCellsPresenter>(rowContainer);
DataGridCell cell =(DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
if(cell == null)
{
dataGrid_Budget.ScrollIntoView(rowContainer,dataGrid_Budget.Columns [column]);
cell =(DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
}
返回单元格;
}
返回null;
}
public DataGridRow GetRow(int index)
{
DataGridRow row =(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);
}
返回行;
}
public static T GetVisualChild< T>(Visual parent)其中T:Visual
{
T child = default(T);
int numVisuals = VisualTreeHelper.GetChildrenCount(parent); (int i = 0; i< numVisuals; i ++)
{
Visual v =(Visual)VisualTreeHelper.GetChild(parent,i);
child = v as T;
if(child == null)
{
child = GetVisualChild< T>(v);
}
if(child!= null)
{
break;
}
}
return child;
}
Hi Guys I am trying to understand how to save and edited row to the db
private void BudgetGrid_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e)
{
SqlCommand gridcmd = new SqlCommand();
SqlConnection rwConn = null;
rwConn = new SqlConnection("server=localhost;" + "Trusted_Connection=yes;" + "database=Production; " + "connection timeout=30");
gridcmd.Connection = rwConn;
rwConn.Open();
//gridcmd.CommandText = "SELECT Id, Name, Quantity, Rate, Time FROM Budget";
gridcmd.CommandText = "UPDATE Budget SET Id = @id, Name = @Name, Quantity = @Qty, Rate = @Rte WHERE Time = @Time";
SqlDataAdapter gridda = new SqlDataAdapter(gridcmd);
string strId = "@id".ToString();
int intID;
bool bintID = Int32.TryParse(strId, out intID);
string strName = "@Name".ToString();
string strQty = "@Qty".ToString();
int intQty;
bool bintQty = Int32.TryParse(strQty, out intQty);
string strRte = "@Rte".ToString();
int intRte;
bool bintRte = Int32.TryParse(strRte, out intRte);
string strTime = "@Time".ToString();
gridda.SelectCommand.Parameters.Add(new SqlParameter("@id", SqlDbType.Int));
gridda.SelectCommand.Parameters["@id"].SqlValue = intID;
gridda.SelectCommand.Parameters.Add(new SqlParameter("@Name", SqlDbType.VarChar));
gridda.SelectCommand.Parameters["@Name"].SqlValue = strName;
gridda.SelectCommand.Parameters.Add(new SqlParameter("@Qty", SqlDbType.Int));
gridda.SelectCommand.Parameters["@Qty"].SqlValue = strQty;
gridda.SelectCommand.Parameters.Add(new SqlParameter("@Rte", SqlDbType.Int));
gridda.SelectCommand.Parameters["@Rte"].SqlValue = strRte;
gridda.SelectCommand.Parameters.Add(new SqlParameter("@Time", SqlDbType.VarChar));
gridda.SelectCommand.Parameters["@Time"].SqlValue = strTime;
DataTable griddt = new DataTable("Budget");
gridda.Fill(griddt);
gridda.UpdateCommand = new SqlCommandBuilder(gridda).GetUpdateCommand();
BudgetGrid.ItemsSource = griddt.DefaultView;
gridda.Update(griddt);
rwConn.Close();
}
it displays fine. I can edit its but when I click on the other tab it does not update it goes back to the original data.
Most of the code I have been going through its either out dated.. or not what I am looking for.
so here is the database
and here is the app
so basically if i hit tab to the next row. under the event BudgetGrid_RowEditEnding
it should update the database.. but now its not.
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;
private void Window_Loaded(object sender, RoutedEventArgs e)
{
SqlConnection Conn = new SqlConnection();
Conn.ConnectionString = yourConnectionString;
Conn.Open();
SqlCommand gridcomm = new SqlCommand();
gridcomm.Connection = Conn;
gridcomm.CommandText = "SELECT Id, Name, Quantity, Rate, Time FROM Budget";
da = new SqlDataAdapter(gridcomm);
SqlDataReader gridreader = gridcomm.ExecuteReader();
while (gridreader.Read())
{
}
gridreader.Close();
dt= new DataTable("Budget");
da.Fill(dt);
dataGrid_Budget.ItemsSource = dt.DefaultView;
Conn.Close();
}
private void dataGrid_Budget_RowEditEnding(object sender, System.Windows.Controls.DataGridRowEditEndingEventArgs e)
{
DataGridRow editedrow = e.Row;
int row_index = (DataGrid)sender).ItemContainerGenerator.IndexFromContainer(editedrow);
for (int k=0;k< 5;k++)
{
DataGridCell cell = GetCell(row_index, k);
TextBlock tb = cell.Content as TextBlock;
if (k==1)
{
dt.Rows[row_index][k] = tb.Text;
}
else if (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 = new SqlCommandBuilder(da).GetUpdateCommand();
da.Update(dt);
}
public DataGridCell GetCell(int row, int column)
{
DataGridRow rowContainer = GetRow(row);
if (rowContainer != null)
{
DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(rowContainer);
DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
if (cell == null)
{
dataGrid_Budget.ScrollIntoView(rowContainer, dataGrid_Budget.Columns[column]);
cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
}
return cell;
}
return null;
}
public DataGridRow GetRow(int index)
{
DataGridRow row = (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;
}
public static T GetVisualChild<T>(Visual parent) where T : Visual
{
T child = default(T);
int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < numVisuals; i++)
{
Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
child = v as T;
if (child == null)
{
child = GetVisualChild<T>(v);
}
if (child != null)
{
break;
}
}
return child;
}
这篇关于更新DATAGRID行以保存到SQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!