vendredi 18 avril 2014

.NET - Windows Form c# datagridview lié aux-mise à jour et économisez - Stack Overflow


I'm pretty new to C#, so I hope to get a little coaching on a windows form bound to database. Here's the general flow about creating a new item master:


1) Review the open sales orders that need a new row. I use a listView showing Item# and description. On SelectedIndexChanged I populate some not-enabled rows with data that pertains to the item being added through one datasource. Also a row is added to a DataGridView with 'default' values assigned programatically using a different datasource.


The above works great...


2) The user would then modify the default suggestions, and save the page and the data should be committed to the database.


The commit has problems....


I will pull in the appropriate sections of code here parsing out the stuff that works...


Find Rows to process:


   listView1_SelectedIndexChanged(...)
{

itemSelected.Text = listView1.SelectedItems[0].Text;

//Fill by item - use different datasets avoiding key conflicts
this.itemTableAdapter.FillByItem(this.COMPANY01DataSet.item, itemSelected.Text);
this.headerTableAdapter.FillByItem(this.itemDataSet.header, itemSelected.Text);

/// Assign default values as suggestions
this.PopHdrDGV();
}

Assign default values & required data:


private void PopHdrDGV()
{
// set defaults as suggestions
DataRow hdrRow = itemDataSet.header.NewheaderRow();
hdrRow["business_unit"] = "US01";
hdrRow["item"] = "item to add";
hdrRow["revision"] = "revision to add";
hdrRow["qty"] = 10;
hdrRow["uom"] = "EA";
hdrRow["cost_flag"] = false;
hdrRow["planning_flag"] = false;
hdrRow["eff_status"] = "N";
hdrRow["eff_date"] = DateTime.Now;
hdrRow["created_by"] = Environment.UserName;
hdrRow["created_date"] = DateTime.Now;
hdrRow["changed_by"] = Environment.UserName;
hdrRow["changed_date"] = DateTime.Now;

//rebind to dgv
headerBindingSource.DataSource = hdrRow;
headerDataGridView.DataSource = headerBindingSource;
}

Save the DGV and commit to database:


    button1_click(....)
{
Validate();
headerBindingSource.EndEdit();
headerTableAdapter.Update(itemDataSet.header);
headerDataGridView.Update(); /// added to test a few different approaches
}

I know this is a pretty basic flow, but in all the research that I've conducted I haven't found anything. All help is gratefully appreciated.




If you are using MSSQL as Database I suggest DataTable And DataAdapter approach:


    SqlDataAdapter adt = new SqlDataAdapter(PredefinedSqlCommand);
DataTable MyDataTable = DataGrid.DataSource as DataTable;
adt.Update(MyDataTable);
MyDataTable.AcceptChanges();

Before this you have to bind table column to SqlCommand and declare it as adapters InsertCommand. The same approach is for updating and deleting rows.


Good luck.



I'm pretty new to C#, so I hope to get a little coaching on a windows form bound to database. Here's the general flow about creating a new item master:


1) Review the open sales orders that need a new row. I use a listView showing Item# and description. On SelectedIndexChanged I populate some not-enabled rows with data that pertains to the item being added through one datasource. Also a row is added to a DataGridView with 'default' values assigned programatically using a different datasource.


The above works great...


2) The user would then modify the default suggestions, and save the page and the data should be committed to the database.


The commit has problems....


I will pull in the appropriate sections of code here parsing out the stuff that works...


Find Rows to process:


   listView1_SelectedIndexChanged(...)
{

itemSelected.Text = listView1.SelectedItems[0].Text;

//Fill by item - use different datasets avoiding key conflicts
this.itemTableAdapter.FillByItem(this.COMPANY01DataSet.item, itemSelected.Text);
this.headerTableAdapter.FillByItem(this.itemDataSet.header, itemSelected.Text);

/// Assign default values as suggestions
this.PopHdrDGV();
}

Assign default values & required data:


private void PopHdrDGV()
{
// set defaults as suggestions
DataRow hdrRow = itemDataSet.header.NewheaderRow();
hdrRow["business_unit"] = "US01";
hdrRow["item"] = "item to add";
hdrRow["revision"] = "revision to add";
hdrRow["qty"] = 10;
hdrRow["uom"] = "EA";
hdrRow["cost_flag"] = false;
hdrRow["planning_flag"] = false;
hdrRow["eff_status"] = "N";
hdrRow["eff_date"] = DateTime.Now;
hdrRow["created_by"] = Environment.UserName;
hdrRow["created_date"] = DateTime.Now;
hdrRow["changed_by"] = Environment.UserName;
hdrRow["changed_date"] = DateTime.Now;

//rebind to dgv
headerBindingSource.DataSource = hdrRow;
headerDataGridView.DataSource = headerBindingSource;
}

Save the DGV and commit to database:


    button1_click(....)
{
Validate();
headerBindingSource.EndEdit();
headerTableAdapter.Update(itemDataSet.header);
headerDataGridView.Update(); /// added to test a few different approaches
}

I know this is a pretty basic flow, but in all the research that I've conducted I haven't found anything. All help is gratefully appreciated.



If you are using MSSQL as Database I suggest DataTable And DataAdapter approach:


    SqlDataAdapter adt = new SqlDataAdapter(PredefinedSqlCommand);
DataTable MyDataTable = DataGrid.DataSource as DataTable;
adt.Update(MyDataTable);
MyDataTable.AcceptChanges();

Before this you have to bind table column to SqlCommand and declare it as adapters InsertCommand. The same approach is for updating and deleting rows.


Good luck.


0 commentaires:

Enregistrer un commentaire