How to cause a Control follow its DataSource? - c#

At the beginning, when I assign my collection to a control's DataSource, it follows what is in the collection. During the execution of my program, when the collection changes, I don't see any change in the control's content. How can I make the control follow the latest changes of the collection?
What I have is:
private void FormEditImages_Load(object sender, EventArgs e)
{
radListView1.DataSource = WebServiceHelper.Instance.CurrentSession.Images;
radListView1.DisplayMember = "Name";
radListView1.ValueMember = "Id";
}
private void radListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
{
var cameraOverlayImage = e.Item.DataBoundItem as CameraOverlayImage;
e.Item.Image = CreateThumbnail(cameraOverlayImage.Image, e.ListViewElement.ItemSize.Width - 12, e.ListViewElement.ItemSize.Height - 16, e.ListViewElement.BackColor);
e.Item.TextAlignment = ContentAlignment.MiddleCenter;
e.Item.TextImageRelation = TextImageRelation.ImageAboveText;
e.Item.ImageAlignment = ContentAlignment.MiddleCenter;
}
How can I change the ItemDataBound method when I change Load to:
private void FormEditImages_Load(object sender, EventArgs e)
{
var bindingSource = new BindingSource(WebServiceHelper.Instance.CurrentSession.Images, "Name");
radListView1.DataSource = bindingSource;
}

Related

Why does grid row change?

I have a asp.net page with a grid and, above the grid, fields showing the contents of the highlighted row and some buttons (edit, add, delete, etc...).
If the user clicks the edit button then it enables the fields for editing.
However, after enabling the fields the grid is then refreshed or something and the first row is then highlighted and the fields on the screen changes. Why? How do I stop this?
Code is below:
protected void btnEdit_Click(object sender, EventArgs e)
{
ChangeButtons(null, EventArgs.Empty);
ChangeFields(null, EventArgs.Empty);
updatetype = "Edit";
}
protected void ChangeButtons(object sender, EventArgs e)
{
btnAdd.Enabled = !btnAdd.Enabled;
btnCopy.Enabled = !btnCopy.Enabled;
btnEdit.Enabled = !btnEdit.Enabled;
btnDelete.Enabled = !btnDelete.Enabled;
btnCancel.Enabled = !btnCancel.Enabled;
btnSave.Enabled = !btnSave.Enabled;
btnClose.Enabled = !btnClose.Enabled;
btnSearchCredits.Enabled = !btnSearchCredits.Enabled;
btnClearCredits.Enabled = !btnClearCredits.Enabled;
}
protected void ChangeFields(object sender, EventArgs e)
{
txtSrchCredible_ID.Enabled = !txtSrchCredible_ID.Enabled;
txtSrchADP_ID.Enabled = !txtSrchADP_ID.Enabled;
txtSrchStart_Date.Enabled = !txtSrchStart_Date.Enabled;
txtSrchEnd_Date.Enabled = !txtSrchEnd_Date.Enabled;
txtCredible_ID.Enabled = !txtCredible_ID.Enabled;
txtADP_ID.Enabled = !txtADP_ID.Enabled;
txtEmployee_ID.Enabled = !txtEmployee_ID.Enabled;
txtStart_Date.Enabled = !txtStart_Date.Enabled;
txtEnd_Date.Enabled = !txtEnd_Date.Enabled;
txtReason.Enabled = !txtReason.Enabled;
txtUnits.Enabled = !txtUnits.Enabled;
txtPercentage.Enabled = !txtPercentage.Enabled;
txtOther.Enabled = !txtOther.Enabled;
}

How change data grid view datasource with ObjectDataSource?

I have two ObjectDataSources in my form.i want change data grid view datasource with ObjectDataSources.
Button's eventhandler to change ObjectDataSource1 is :
protected void lbnChangeInfo1_Click(object sender, EventArgs e)
{
gdvList.DataSource = null;
gdvList.DataBind();
gdvList.DataSource = odsWork1;
gdvList.DataBind();
}
Button's eventhandler to change ObjectDataSource2 is :
protected void lbnChangeInfo2_Click(object sender, EventArgs e)
{
gdvList.DataSource = null;
gdvList.DataBind();
gdvList.DataSource = odsWork2;
gdvList.DataBind();
}
When executed, this error is thrown:
Additional information: Both DataSource and DataSourceID are defined
on 'gdvList'. Remove one definition.
change code with :
protected void lbnChangeInfo1_Click(object sender, EventArgs e)
{
gdvList.DataSource = null;
gdvList.DataSourceID = null;
gdvList.DataSource = odsWork1;
gdvList.DataBind();
}

C# combobox selected index changed fires old value in another combobox

I have found the closest reference of my problem on these links:
ComboBox has its old value after Clear()
Why selected index fires only once the Index of the ListItem is changed?
I have 4 comboboxes: cmbcountry, cmbstate, cmbdistrict and cmbcity. cmbcountry populates on load event by GetCountry() method. cmbstate populates by GetState(countryid) method, which takes cmbcountry's selectedvalue as argument and returns a list of relevant states and so on for cmbdistrict and cmbcity...
Problem is on selecting different item in cmbState, cmbDistrct is not populating with the proper items.
.E.g. states :"madhya pradesh", "Utter pradesh" , "Rajsthan"
For "Rajsthan", cmbdistrict is populating with relavent value, but for "Utter pradesh", cmbdistrict still has old value.
Here is the relevant code:
private void TestForm1_Load(object sender, EventArgs e)
{
cmbCountry.ItemSource = Lookups.Lookup.GetCountries();
}
private void cmbCountry_SelectionChangeCommitted(object sender, EventArgs e)
{
cmbState.Text = "";
cmbState.Clear();
cmbState.SelectedIndex = -1;
cmbState.SelectedItem = null;
//cmbState.Items.Clear();
int countryId = Convert.ToInt32(cmbCountry.SelectedValue);
cmbState.ItemSource = Lookups.Lookup.GetStates(countryId);
}
private void cmbState_SelectionChangeCommitted(object sender, EventArgs e)
{
cmbDistrict.Text = "";
cmbDistrict.Clear();
cmbDistrict.SelectedIndex = -1;
cmbDistrict.SelectedItem = null;
//cmbDistrict.Items.Clear();
int stateId = Convert.ToInt32(cmbState.SelectedValue);
cmbDistrict.ItemSource = Lookups.Lookup.GetDistricts(stateId);
}
private void cmbDistrict_SelectionChangeCommitted(object sender, EventArgs e)
{
cmbCity.Text = "";
cmbCity.Clear();
cmbCity.SelectedIndex = -1;
cmbCity.SelectedItem = null;
//cmbCity.Items.Clear();
int DistrictId = Convert.ToInt32(cmbDistrict.SelectedValue);
cmbCity.ItemSource = Lookups.Lookup.GetCities(DistrictId);
}
private void cmbBank_SelectionChangeCommitted(object sender, EventArgs e)
{
int bankId = Convert.ToInt32(cmbBank.SelectedValue);
cmbControlBranch.ItemSource = Lookups.Lookup.GetBranches(bankId);
}
I have all above methods to clear previous data in comboboxes ...and items.clear() is giving an error if i am using it.
Please tell me if there is any other relevant reference which I am missing .

DevExpress DataBinding, Add new Record

I am using DevExpress in my winform application, I have a gridview, data entry form, datanavigator, all bound to dataset.
I want to add new record, if using datanavigator "Add" it works good, how to do the same using a "New Record" button?
BindingSource.AddNew()
is not working, it usually does, but with devexpress its not working.
If you want to use binding then use your objects with binding source..
and use the binding list .AddingNew += new AddingNewEventHandler(listOfParts_AddingNew);
event to add new entity object ..
See the example of BindingList on MSDN.
void listOfParts_AddingNew(object sender, AddingNewEventArgs e)
{
e.NewObject = new Part(textBox1.Text, int.Parse(textBox2.Text));
}
DevExpress WinForm Controls works so fast with binding sources as compare to typed datasources etc... YOu can implement bindingSources using these example..
set gridview and the associcated controls datasource to bindsouce that you have created...
process your form with the this MSDN example..
have a look on this code snippet.. may be you will get some idea from this..
private void BindingLIstDemo_Load(object sender, EventArgs e)
{
InitializeListOfEmployees();
BindlstEmp();
listofEmp.AddingNew += new AddingNewEventHandler(listOfEmp_AddingNew);
listofEmp.ListChanged += new ListChangedEventHandler(listofEmp_ListChanged);
}
private void BindlstEmp()
{
lstEmpList.Items.Clear();
lstEmpList.DataSource = listofEmp;
lstEmpList.DisplayMember = "Name";
}
void listofEmp_ListChanged(object sender, ListChangedEventArgs e)
{
MessageBox.Show(e.ListChangedType.ToString());
//throw new NotImplementedException();
}
//declare list of employees
BindingList<Emp> listofEmp;
private void InitializeListOfEmployees()
{
//throw new NotImplementedException();
// Create the new BindingList of Employees.
listofEmp = new BindingList<Emp>();
// Allow new Employee to be added, but not removed once committed.
listofEmp.AllowNew = true;
listofEmp.AllowRemove = true;
// Raise ListChanged events when new Employees are added.
listofEmp.RaiseListChangedEvents = true;
// Do not allow Employee to be edited.
listofEmp.AllowEdit = false;
listofEmp.Add(new Emp(1, "Niranjan", 10000));
listofEmp .Add (new Emp (2,"Jai", 8000));
}
// Create a new Employee from the text in the two text boxes.
void listOfEmp_AddingNew(object sender, AddingNewEventArgs e)
{
e.NewObject = new Emp (Convert.ToInt32(txtId.Text), txtName.Text,Convert.ToInt32(txtSalary.Text));
}
private void btnAdd_Click(object sender, EventArgs e)
{
Emp empItem = listofEmp.AddNew();
txtId.Text = txtName.Text = txtSalary.Text = "";
}
private void button1_Click(object sender, EventArgs e)
{
Form1 obj = new Form1();
obj.Show();
}
private void btnDelete_Click(object sender, EventArgs e)
{
var sg = (from sc in listofEmp.ToList<Emp>() where sc.Name == ((Emp)lstEmpList.SelectedValue).Name select sc);
}
private void lstEmpList_SelectedIndexChanged(object sender, EventArgs e)
{
Emp se = listofEmp[lstEmpList.SelectedIndex];
txtId.Text = se.Id.ToString();
txtName.Text = se.Name;
txtSalary.Text = se.Salary.ToString();
}
Here I am using BindingList as datasouce BindingList<Emp> listofEmp; and on the place of grid listing of records are shown in a listbox control.. but all same...try this with your gridview..

Using a control created on Form_Load in another event

I have two controls created on Form_Load, a button and a combobox. I also have an event for the button, but the event should be able to see the newly created combobox.
When I try to call the combobox by it's name it says that it doesn't exist in this context.
private void Form1_Load(object sender, EventArgs e)
{
Button przycisk = new Button();
przycisk.Name = "przycisk";
przycisk.Dock = DockStyle.Bottom;
przycisk.Text = "Wybierz";
ComboBox kombo = new ComboBox();
kombo.Name = "kombo";
kombo.Dock = DockStyle.Bottom;
kombo.Items.Add("Przycisk");
kombo.Items.Add("Etykeita");
kombo.Items.Add("Pole tekstowe");
Controls.Add(kombo);
Controls.Add(przycisk);
przycisk.Click += new EventHandler(przycisk_Click);
}
private void przycisk_Click(object sender, EventArgs e)
{
kombo.Items.Add("Panel"); //just an example
}
Is there a way to make this work?
Only controls which are in used in markup with runat="server" will be class variables on your page. They are actually defined in the designer file.
What you'll want to do is in the class add something like the following where you have a class variable, then assign kombo in your page-load function. Then, it will exist in your click event handler.
// kombo is now scoped for use throughout this class
ComboBox kombo = null;
private void Form1_Load(object sender, EventArgs e)
{
Button przycisk = new Button();
przycisk.Name = "przycisk";
przycisk.Dock = DockStyle.Bottom;
przycisk.Text = "Wybierz";
// Assign to our kombo instance
kombo = new ComboBox();
kombo.Name = "kombo";
kombo.Dock = DockStyle.Bottom;
kombo.Items.Add("Przycisk");
kombo.Items.Add("Etykeita");
kombo.Items.Add("Pole tekstowe");
Controls.Add(kombo);
Controls.Add(przycisk);
przycisk.Click += new EventHandler(przycisk_Click);
}
private void przycisk_Click(object sender, EventArgs e)
{
// Using the kombo we created in form load, which is still referenced
// in the class
kombo.Items.Add("Panel"); //just an example
}
You will have to use the FindControl() method to find the object first.
private void przycisk_Click(object sender, EventArgs e)
{
ComboBox kombo = (ComboBox)FindControl("kombo");
kombo.Items.Add("Panel");
}

Categories

Resources