Change datasource on button click in Winforms app - c#

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;
}

Related

C# Permanently Add and Save item to combobox

everybody!
I have collection of items in combobox's properties. And I want to add new item in my combobox by writing text in combobox and then use button event:
private void button2_Click_1(object sender, EventArgs e)
{
cbx_unix_dir.Items.Add(cbx_unix_dir.Text);
}
But in next time of starting my programm - my added item doesn't exist in combobox. What do I wrong? I need all added items have been saved in my combobox for ever. May be problem in method InitializeComponents()? May be I have to add event before it?
Thank you very much.
ComboBox has no functionality to save and reload items.
You may store items into .NET Settings file on closing window and reload them on loading form:
private void Form1_Load(object sender, EventArgs e)
{
if (Settings.Default.cboCollection != null)
this.cbx_unix_dir.Items.AddRange(Settings.Default.cboCollection.ToArray());
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
ArrayList arraylist = new ArrayList(this.cbx_unix_dir.Items);
Settings.Default.cboCollection = arraylist;
Settings.Default.Save();
}
//A button to add items to the ComboBox
private void button2_Click_1(object sender, EventArgs e)
{
cbx_unix_dir.Items.Add(cbx_unix_dir.Text);
}

DevExpress CustomRowFilter - how to trigger?

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;
}

C# - Binding FormClosing event to cause Form refresh

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

Refresh DataGridView using BindingSource and TableAdapter

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.

how to bind more than one dropdownlist without refreshing?

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

Categories

Resources