uable to retrieve radio button value from database - c#

I have a method called void showdata() and If I do this...
textBox1.Text = ds.Tables[0].Rows[rno][0].ToString();
comboBox1.Text = ds.Tables[0].Rows[rno][1].ToString();
I am able to retrieve values from database but I am unable to retrieve value from radiobuttons e.g.
rBMale.Text = ds.Tables[0].Rows[rno][3].ToString();
by using navigation buttons.
When I say values I mean the green dot that you see from the application not the string value. I have named rBMale as radiobutton.
Does anyone know why that is?
Thanks

It is not clear, but it seems that you want to set the Checked property of the RadioButton.
rBMale.Checked = Convert.ToBoolean(ds.Tables[0].Rows[rno][3]);
This property is used to paint the RadioButton with the glyph to represent the value for true or for false

The Text property of radio buttons and check boxes is the on screen text that tells the user what the button is (in theory) and has little or nothing to do with its value. What you are probably looking for is the Checked property, which is a bool.
try something along these lines:
rBMale.Checked = Convert.ToBoolean(ds.Tables[0].Rows[rno][3]);

Related

x.Text not updating after assigning default value to it

I programmed in VS2013 using C#. I tried to modified a drop down textbox (Telerik RadDropDownListElement). Last user's input will be the default value when opening the software.
I used Properties.Settings.Default.var to save the user input. when I assign it to x.Text, I can no longer access the value in textbox entered by the user in run time.
public Initialize()
{
/****other initial ****/
x.Text = Properties.Settings.Default.var;
// set text to default value
}
private void save_button_Click(object sender, EventArgs e)
{
Properties.Settings.Default.var = x.Text;
//x.Test still has the default value, not the value in textbox
Properties.Settings.Default.save();
}
I tried to use property binding from following link, but I get similar result.
Automatically update the Application Setting using the binding from VS.Net Designer
How can I access the value in textbox instead of default value when I click save button?
/****edit detail****/
I can save x.Text to var if I comment out
x.Text = Properties.Settings.Default.var;
but then the default value is not shown.
I used drop down list as a simple textbox. Will this make a significant difference?
I'm not using index of drop down list, just the text.
I guess it depends how did you populate the drop down list. If it is in unbound mode and you have added the items manually, you could set the Text of the control, but if it is bound, you should work with Value.
However, I would suggest different approach. Instead of setting Text, set the SelectedItem (or SelectedIndex properties). To set the SelectedItem, find the item you need to select and assign it to the property:
radDropDownList1.SelectedItem = radDropDownList1.FindItemExact("your string", true);
Make sure the FindItemExact returns an item, if not, check your items and the saved string you have.
There are also couple other Find* methods you could use. They return index, that can be set in the SelectedIndex property.
Try Properties.Settings.Default["mysetting"];

Unable to set Text in Combox Box in DropDownList

I want to set the text in a combo box. Below is the code-
private System.Windows.Forms.ComboBox selectModel;
this.selectModel = new System.Windows.Forms.ComboBox();
this.selectModel.Name = "selectModel";
this.selectModel.FormattingEnabled = true;
this.selectModel.Size = new System.Drawing.Size(64, 21);
this.selectModel.Location = new System.Drawing.Point(3, 76);
this.selectModel.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
The following line is not working-
selectModel.SelectedText = getModelNameFromConf();
The documentation says that "it Gets or sets the text that is selected in the editable portion of a ComboBox". I can't make it editable to user.
Any workaround please.
This is because when you use ComboBoxStyle.DropDownList, the dropdown has no editable portion. To make it editable, use ComboBoxStyle.DropDown.
Note too the remarks on the SelectedText property relating to whether the control has focus. You may find the Text property more suitable for many purposes.
EDIT For example:
selectModel.Text = getModelNameFromConf();
Assuming the combo contains that value in its list, setting Text will also set the SelectedIndex property of the dropdown.
(I think some of the property names of this control are particularly confusing, including DropDown vs. DropDownList. Someone at MS had a bad day when this control was coded. Note also that the word selection is being used in two different ways: here, you want to set the selected item, whereas SelectedText means some text that is selected—which might not be the whole of the item text. This is the same as in a textbox where the user has dragged the mouse to highlight some of the text but not all of it.)

C# Datagridview: get selected item in combobox columns

I'm working on a GUI that allows the user to manipulate xml files. I display the xml file in a datagridview organized neatly by columns through xml elements. I allow the user to add columns as an extention on my project. The column gets added to the dataset table, then updated to the datagridveiew that I use to display the xml file in. I've included the ability for the user to add a combobox column to select choices instead of entering them in constantly like.. true or false. However, that is where the problem lies. Saving a normal column was easy. The combobox column is being a pain.
I have a "save combobox column" to have it updated to the xml and a "save" button to save in a destination of the user's choice.
I've done some research and it seems like the combobox class has such a feature to gain access to the selecteditem in the combobox put in by the user.
Where we have:
ComboBox box = new ComboBox();
box.SelectedItem;
I tried applying this to the combobox column class but it does not have such a function. Thus, I cannot figure out how to directly obtain the value of the user's selected item. I tried experimenting with comboboxcell's as well, but that didn't lead me anywhere either. Both those classes I played around with do not have a... "selected item" function and even google does not have a solution for me. =( I've also tried using the cell.value, but it is "null" for some reason. Even when the user selects an item in the box, it doesn't get saved into the cell's value.
TLDR:
My question in short is, how, if possible, do you gain access to the comboboxcolumn cell's selected item? Additionally, how would you then ensure that the item value is saved in the cell?
Thanks in advance. I'm using .NET 3.5 SP1, through Visual Studio 2008 C#.
Sincerely,
tf.rz
The Control in a DataGridView is not a ComboBox, it is a DataGridViewComboBox and has different properties and methods. From MSDN
Unlike the ComboBox control, the DataGridViewComboBoxCell does not have SelectedIndex and SelectedValue properties. Instead, selecting a value from a drop-down list sets the cell Value property.
However, you mentioned that the Cell.Value is null for you. Well there may be another step you are missing according to the following article (How to: Access Objects in a Windows Forms DataGridViewComboBoxCell Drop-Down List).
You must set the DataGridViewComboBoxColumn.ValueMember or DataGridViewComboBoxCell.ValueMember property to the name of a property on your business object. When the user makes a selection, the indicated property of the business object sets the cell Value property.
If we have bound a datagridcomboboxcell with a different DisplayMember and ValueMember, like so:
dgcombocell.DisplayMember = "Name";
dgcombocell.ValueMember = "Id";
dgcombocell.DataSource = dataset1.Tables[0];
Then for getting SelectedText, and SelectedValue, we can write this code:
string SelectedText = Convert.ToString((DataGridView1.Rows[0].Cells["dgcombocell"] as DataGridViewComboBoxCell).FormattedValue.ToString());
int SelectedVal = Convert.ToInt32(DataGridView1.Rows[0].Cells["dgcombocell"].Value);
I hope it solves your problem.
Use this to get or set selected value:
object selectedValue = currentRow.Cells["comboboxColumnName"].Value
Don't forget to set DisplayMember and ValueMember for your DataGridViewComboBoxColumn
This is how it is done
DataGridViewComboBoxCell comboCell = (DataGridViewComboBoxCell)dgv.Rows[0].Cells[1];
MessageBox.Show(""+comboCell.Items.IndexOf(comboCell.Value));
A .Net combox is actually a composite control made up of a textbox and a dropdownlist. Use box.Text to get the currently displayed information.
EDIT: The row or the cell should have a .FindControl() method. You'll need to do something like:
Combobox box = (Combobox)(row.FindControl("[combobox ID]"));
string val = box.Text;
Basically, you're finding the control within its container (row or cell), then casting the control found as a combobox, then accessing its .Text property.
I use this:
private int GetDataGridViewComboBoxCellSelectedIndex(DataGridViewCell d)
{
return ((DataGridViewComboBoxCell)d).Items.IndexOf(d.Value);
}

Different Value Member, same control

Edit 1
I believe my problem stems from the following. The function that fills the dropdown portion sets the Display Member to the CountyName. Then when I try and set the SelectedText or EditValue, as has been suggested, that function only returns the CountyID which it try's to match to something in the DropDown list DisplayMember. I need it to match it to something in the ValueMember list.
Using the following I got it to work but it is a HACK and I'd greatly appreciate finding a real solution.
lkuResidenceCounty.ItemIndex = Convert.ToInt32(row["ResidencyCountyID"].ToString());
Original Post
I have a lookup box(DevExpress) on a member form that I fill the possible values in from the DB with this code -->
lkuResidenceCounty.Properties.DataSource = ConnectBLL.BLL.Person.CountyList();
lkuResidenceCounty.Properties.PopulateColumns();
lkuResidenceCounty.Properties.DisplayMember = "CountyName";
lkuResidenceCounty.Properties.ValueMember = "CountyID";
lkuResidenceCounty.Properties.Columns[0].Visible = false;
lkuResidenceCounty.Properties.Columns[2].Visible = false;
lkuResidenceCounty.Properties.Columns[3].Visible = false;
This works just fine as the CountyName is displayed as expected.
However, When I try and load an existing member's value for this field using the below, which is part of a function that takes a row from the DataSet -->
lkuResidenceCounty.Properties.ValueMember = row["ResidencyCountyID"].ToString();
I get a blank box. I have stepped through the code and the correct ID is being returned for the member.
Unfortunately the stored procedure to fill the dropdown options pulls from a Maintenance Table with the columns "CountyName" & "CountyID". So that is correct. Unfortunately, the stored procedure to load a specific person's current county pulls from the Person Table where there is a column called "ResidencyCountyID". It is so named because there is also a "ResponsibilityCountyID" column.
I need a way for them both to coexist, any solutions?
Thanks!
DisplayMember and ValueMember are used to populate the control with the list of selectable values. To set the selected value of a populated LookUpEdit control, set its EditValue property:
lkuResidenceCounty.EditValue = row["ResidencyCountyID"].ToString();
In response to your edit: According to the documentation:
The currently selected row determines values for the editor's edit value and display text. The value for BaseEdit.EditValue is obtained from the RepositoryItemLookUpEditBase.ValueMember field, while the text to display in the edit box is obtained from the RepositoryItemLookUpEditBase.DisplayMember field of the selected row.
When you change BaseEdit.EditValue,
the editor locates and selects the row
whose
RepositoryItemLookUpEditBase.ValueMember
field contains the new value. The text
in the edit box is changed to reflect
the newly selected row.
I don't use these controls but it sounds to me that it shouldn't be working as you described. I think the ToString() is the problem because EditValue accepts an object so it's probably expecting an int for the value. Try:
lkuResidenceCounty.EditValue = (int)row["ResidencyCountyID"];
The ValueMember property tells the list what field to pull from when setting the Value property. Once you've set the ValueMember to "CountyID", then when the list is Databound, it will set all the list items' value properties to their respect objects' CountyID fields.
Hence, you should not do:
lkuResidenceCounty.Properties.ValueMember = row["ResidencyCountyID"].ToString();
but rather
lkuResidenceCounty.Properties.ValueMember = "CountyID";
was perfectly fine, as long as you've correctly identified the field you're trying to databind. Once the list get's databound you should see the results you're expecting.
However, after looking at your code, it seems that you're mismatching your field. In one place you're using ResidencyCountyID and in another you're using CountyID. That is most likely your source of confusion. Figure out what the actual field name is and make sure you set the ValueMember to that.
UPDATE
After reading your comment, what your looking for is the SelectedValue property. The SelectedValue property tells the list to force the selected value to whatever input you give it.
Essentially you have two things going on here. ValueMember tells the list what to use as the value from your data source, and SelectedValue which tells the list what the currently selected value should be.
Why is it that you need the same LookUpEdit to have two value members? Is it being used standalone or in a grid? If standalone, you could swap out the two repository editors depending on the current row. But, are there more than 2 possible values for the ValueMember? That would also complicate things.
UPDATE
Looking at your edit, I think I understand what's going on a little more. So, you don't wish to change your ValueMember (which refers to a data column), but rather to change the value of the editor? If so, then you should definitely use EditValue (not SelectedText, which I don't believe is meant to be set), and assign it to row["value_field"] like so:
lkuResidenceCounty.EditValue = row["ResidencyCountyID"];
What happens when you do that?

How do I set a ComboBox default *not in the drop down* when a list item begins with the same text as a drop down item?

Using C#, say you have a ComboBox that has a DropDownStyle set to DropDown (there are items in the drop down but the user can manually enter a value as well). How do you set a default value for the ComboBox that is not in the list of values in the drop down, but begins with text from a possible selection? Normally setting ComboBox.Text works fine, but if there is an item in the drop down that begins with the text you want as the default, it automatically selects the first item in the list that starts with the text. For example:
Values in the drop down:
c:\program files\
c:\windows\
d:\media\
Default Value Assignment
myComboBox.Text = "C:\";
Result
Initial value of ComboBox when the form opens is "c:\program files\".
So what am I doing wrong? How do I correctly set a default value of an item not in the drop down list that begins with a possible selection?
Does the following code work?
myCombo.SelectedIndex = myCombo.FindString(#"c:\");
Note: I haven't tried it. Looked up for properties/methods that could help using reflector.
I couldn't repro the behavior you are describing. Adding the three values via the Items collection and then setting the initial value to "c:\" (you omitted an # in your code sample, by the way) worked fine. My guess is that something else in your code is setting the value of the combo box after you set it.
I was able to get this to work with having the items in the ComboBox be ComboBoxItems (I dont see why this wouldn't work with other types). Set the ComboBox.Text like you are and make sure SelectedIndex = -1 and also you need IsEditable = True.

Categories

Resources