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);
}
}
Related
I'm very new to C# programming, so am hoping this is something simple that I'm overlooking.
I have a XtraGrid.GridControl on my form which I want to apply a CustomRowFilter against.
I am not explicitly defining any views for the grid, so I thought I'd be adding my CustomRowFilter method to an event handler against the MainView on my GridControl - however I can't find how to access the event I'm after?
Is this the right approach or am going about this the wrong way?
private void gridControl1_CustomRowFilter(object sender, RowFilterEventArgs e)
{
GridView view = sender as GridView;
DataView dv = view.DataSource as DataView;
if (1==1) //Temp - this should hide everything
{
e.Visible = false;
e.Handled = true;
}
}
private void Form1_Load(object sender, EventArgs e)
{
//Bind the datasource etc...
gridControl1.MainView.CustomRowFilter += gridControl1_CustomRowFilter //"Base view does not contain a definition for "CustomRowFilter"
}
The CustomRowFilter event belongs to the GridView and not to GridControl, You can access the event with :
private void Form1_Load(object sender, EventArgs e)
{
(gridControl1.MainView as GridView).CustomRowFilter += gridControl1_CustomRowFilter;
}
I am having some difficulty getting a form to refresh when another form is closed. This is what I have so far, but it doesn't seem to trigger the refresh. I am very to new to programming, so any assistance is appreciated!
private void button2_Click(object sender, EventArgs e)
{
AddNewCourse ANCform = new AddNewCourse();
ANCform.FormClosing += new FormClosingEventHandler(this.ANC_FormClosing);
ANCform.Show();
}
private void ANC_FormClosing(object sender, FormClosingEventArgs e)
{
this.Refresh();
}
Rebind the datasource of your DataGridView in ANC_FormClosing
For example, if I were rebinding using a method that fetches data, I might write
private void ANC_FormClosing(object sender, FormClosingEventArgs e)
{
DataGridView.DataSource = GetFromDB();
}
This refreshes the grid with new data fresh from the DB
i actually work on a Customer-DataGrid but i stuck on the Sources because i dont use really often C#.
I have a DataGridView (dataGridView1), a internal Database (Database.mdf), a BindingSource (customerBindingSource) and customerTableAdapter
Now i trying to refresh the DataSource when i click a button.
Here is a simple snippet:
private void Kundenverwaltung_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'kundenAnsicht.customer' table. You can move, or remove it, as needed.
this.customerTableAdapter.Fill(this.kundenAnsicht.customer);
}
// I tried already some methods but i dont find a properly, functionally way
private void button2_Click(object sender, EventArgs e)
{
this.customerTableAdapter.Fill(this.kundenAnsicht.customer);
}
I hope you can understand my problem.
~ Dennis
You need to connect your DataGridView with "customerBindingSource":
`private void Kundenverwaltung_Load(object sender, EventArgs e)
{
this.dataGridView1.DataSource = this.customerBindingSource;
this.customerTableAdapter.Fill(this.kundenAnsicht.customer);
}
private void button2_Click(object sender, EventArgs e)
{
this.customerTableAdapter.Fill(this.kundenAnsicht.customer);
}`
It all.
I have a datagridview (dataGridView1) pulling information out of a mdb datasource called rentBindingSource, how can I pull information out of hireBindingSource on a button click i.e.
private void viewHire_Click(object sender, EventArgs e)
{
// refresh datagrid view to load stuff from hireBindingSource
}
Try this:
private void viewHire_Click(object sender, EventArgs e)
{
dataGridView1.DataSource = hireBindingSource;
}
protected void Page_Load(object sender, EventArgs e)
{
bindbranches();
bindbranches1();
}
public void bindbranches()
{
DataTable dtbranch = new DataTable();
dtbranch = objsupplyBAL.getbrnch();
ddlbranch.DataSource = dtbranch;
ddlbranch.DataBind();
ddlbranch.Items.Add(new ListItem("--select--", "0"));
ddlbranch.SelectedIndex = ddlbranch.Items.Count - 1;
}
public void bindbranches1()
{
DataTable dt = new DataTable();
dt = objsupplyBAL.getbrnch();
ddlbranch1.DataSource = dt;
ddlbranch1.DataBind();
ddlbranch1.Items.Add(new ListItem("--select--", "0"));
ddlbranch1.SelectedIndex = ddlbranch1.Items.Count - 1;
}
My dropdownlist's are not binding without refreshing.If i select one dropdownlist another one is refreshing. What i have to add extra to my code. Can any one tell...
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
bindbranches();
bindbranches1();
}
}
if you add this...it's work properly ...first try this...
The reason that is happening is you are running the code every time the page postsback, try the following to only populate the items once (on the initial page load) :
protected void Page_Load(object sender, EventArgs e) {
if (!this.IsPostBack) {
bindbranches();
bindbranches1();
}
}
Alternatively you can also handle the Page.Init event to run this code, this will change the dropdowns when the page is first loaded and will keep the values throughout subsequent postbacks :
protected void Page_Init(object sender, EventArgs e) {
bindbranches();
bindbranches1();
}
If you want the second dropdown to refresh only when the first item is selected, try the following solution :
protected void ddlbranch_SelectedIndexChanged(object sender, EventArgs e) {
bindbranches1();
}
And bind ddlbranch_SelectedIndexChanged to the selected index changed event of your ddlbranch control. This will only run the code when the page is initially loaded and when the user selects an item from the ddlbranch dropdown
You will have to set AutoPostBack Property of the drop down to true if you want to fill another drop down on change of one drop down so on change event will start executing
Loot # http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listcontrol.autopostback.aspx