how to update database from datagridview - c#

I've checked the option Enable Editing in the datagridview
I've a dataset model generated from my database i'm trying to use update method but its not taking the right parameters
on load even it is fetching and showing into datagridview.
private void FrmSession_Load(object sender, EventArgs e)
{
tblSessionTableAdapter tblSession = new tblSessionTableAdapter();
dataGridView1.DataSource = tblSession.spGetSessionRecord();
}
but on Button click even its not updating from datagridview am i missing something?
private void btnAdd_Click(object sender, EventArgs e)
{
FMSDataSet ds = new FMSDataSet(); //Object of automatically generated model
tblSessionTableAdapter tblSession = new tblSessionTableAdapter();
tblSession.Update(ds); // i put ds here because on update's third constructor it requires dataset
}

The problem is that a new DataSet is created in click method. The new DataSet doesn't contains any modification because it's freshly created.
Modifications are contains in the DataSet/DataTable that is the DataSource of the GridView.
tblSession.Update([Put the same DataSet/DataTable as in load method here]);

If you want your data set to have state then you need to make your data set a member variable so you can keep the current state and update it.
FMSDataSet ds;
Constructor()
{
ds = new FMSDataSet();
}
//Then access it in your click event.
private void btnAdd_Click(object sender, EventArgs e)
{
tblSessionTableAdapter tblSession = new tblSessionTableAdapter();
tblSession.Update(this.ds); // update persistent data set
}

Related

DevExpress Gridview can't show data

I have this code :
private void frmWeld_Load(object sender, EventArgs e)
{
List<Weld> lst = _weldRepository.Get().ToList();
gridControl.DataSource = new BindingList<Weld>(lst) { AllowNew = true };
}
I want to load my data into devExpressGridView
As you can see my data is loaded but the gridview can't show the data and my break point doesn't pass from gridControl.DataSource = new BindingList<Weld>(lst) { AllowNew = true} and my program remains with this state.
Why?
I just add new column to my gridview after this the problems occurreded.
I use entity framework ,when i change the database my application creates a new database using code first and after that my data is lost but the problem that i said is solved.
Since your code is executing in Load event, add ForceInitialize() to your code.
private void frmWeld_Load(object sender, EventArgs e)
{
// your previous code
gridControl.ForceInitialize(); <- add this line
}

How to display a datatable in the view?

I have a visual studio windows from called managerForm. Clicking a button on the manager form will bring up a filter form. Also, I have a separate dataGridView Form.
By clicking "SELECT" button on the filter form, I will get a SQL dataTable. I want to display the table on the dataGridView Form.
I don't know how to connect them together. Any help?
dg.myDatagridView.DataSource = filter.returnedDataList;
In this code, I cannot get the returnedDataList since filter closed.
private void btnDisplayFilter_Click(object sender, EventArgs e)
{
filterForm filter = new filterForm();
filter.ShowDialog(this);
displayGridViewControl dg = new displayGridViewControl();
dg.myDatagridView.DataSource = filter.returnedDataList;
displayGridView.ShowDialog();
}
You could try with using statement
private void btnDisplayFilter_Click(object sender, EventArgs e)
{
using(filterForm filter = new filterForm())
{
if(filter.ShowDialog(this) == DialogResult.OK)
{
displayGridViewControl dg = new displayGridViewControl();
dg.myDatagridView.DataSource = filter.returnedDataList;
displayGridView.ShowDialog();
}
}
}
This will keep the instance of filterForm still available and you could get the public properties of this instance until your code reaches the closing brace of the using statement

Update Error in cell

When I edit my DataGridView's cell I want to immediately update my MSSQL database. I added a CellEndEdit' event handler. Also I added autogenerated bindingsource andTableAdapter`.
It works correctly but whenever I want to edit my value I get an error. Actually, I wrote an update method with SqlDataAdapter. But in this time I use auto-generated TableAdapter. I don't know what to do. I try this one but I got an error.
Note: DataSet11, loginTableAdapter, and Bindingsource1 are auto generated and work.
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
SqlCommandBuilder querybuilder = new SqlCommandBuilder(loginTableAdapter);
querybuilder.GetUpdateCommand();
loginTableAdapter.Update(dataset11);
}
private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
if (dataGridView1.IsCurrentCellDirty)
{
SqlCommandBuilder querybuilder = new SqlCommandBuilder(loginTableAdapter);
querybuilder.GetUpdateCommand();
loginTableAdapter.Update(dataset11);
}
}
//or if you has a bindingSource
private void bindingSource1_CurrentItemChanged(object sender, EventArgs e)
{
if (dataGridView1.IsCurrentCellDirty)
{
SqlCommandBuilder querybuilder = new SqlCommandBuilder(loginTableAdapter);
querybuilder.GetUpdateCommand();
loginTableAdapter.Update(dataset11);
}
}

How to set the DataSource of a DataGridView inside a ToolStripControlHost? (C# window forms)

I have a DataGridView inside a ContextMenu control, please see the code snippet below:
private void Form1_Load(object sender, EventArgs e)
{
SetDataSource(dataSet1);// A populated DataSet
}
protected void SetDataSource(DataSet ds)
{
dataGridView1.DataSource = ds;
ToolStripControlHost tsHost = new ToolStripControlHost(dataGridView1);
contextMenuStrip1.Items.Clear();
contextMenuStrip1.Items.Add(tsHost);
contextMenuStrip1.Show(textBox1, 0, 27);
}
private void button1_Click(object sender, EventArgs e)
{
SetDataSource(dataSet2);// Another populated DataSet
}
What happens here is when in the form opens, it shows the contextMenu and display the DataGridView on it with the value of dataSet1. But when I click the button to change the DataSource of the Grid, It doesn't show the records of dataSet2.
Please help me how to fix this... thanks...
You might try setting the DGV's DataSource to a BindingSource object, and then modifying the BindingSource's DataSource instead. You can force the BindingSource to update, if it doesn't automatically, by invoking its CurrencyManager.Refresh().
Answered already here.

Display a deleted DataRow in a (Infragistics.Ultra-) grid

is it possible to display a DataRow from a DataTable which has the DataRowState.Deleted?
Scenario:
The user can edit some kind of lookup-informations which are presented in the grid.
Now he/she can delete, modify or insert multiple entries and finally store all his/her
changes with one click to the database (assuming there is no primary-key-violation
or some other problem).
Now i want to colorize the different rows according to their edit-status, but the
deleted rows disappear immediatly.
Do you have any idea or another approach to solve this problem?
Edit: I realized that the Grid you are using is not DataGridView. For anyone who wants to do the same with DataGridView, they can do the following:
Create a DataView:
DataView myDataView =
new DataView(myDataTable,
String.Empty, // add a filter if you need one
"SortByColumn",
DataViewRowState.OriginalRows | DataViewRowState.Deleted);
myDataGridView.DataSource = myDataView;
Handle UserAddedRow, UserDeletedRow and CellValueChanged Events:
private void myDataGridView_UserAddedRow
(object sender, DataGridViewRowEventArgs e)
{
e.Row.DefaultCellStyle.BackColor = ColorTranslator.FromHtml("#CCFF99");
}
private void myDataGridView_UserDeletedRow
(object sender, DataGridViewRowEventArgs e)
{
e.Row.DefaultCellStyle.BackColor = ColorTranslator.FromHtml("#FFCC99");
}
private void myDataGridView_CellValueChanged
(object sender, DataGridViewCellEventArgs e)
{
myDataGridView[e.ColumnIndex, e.RowIndex].DefaultCellStyle.BackColor
= ColorTranslator.FromHtml("#FFFF99");
}

Categories

Resources