I am working on RadGridView of telerik. I have a "GridViewComboBoxColumn" with a list of strings as data source.
Now the problem is that when I populate the grid view with data, it is possible that the value in data is not available in the strings data source for that column which results in blank value.
I tried setting DropDownStyle to RadDropDownStyle.DropDown but that doesn't change anything. And I just need to display the data even if that is not present in the drop down list.
Here is some code to help you understand it better.
Dim lstValues As New List(Of String)
lstValues.Add("Approved")
lstValues.Add("Declined")
lstValues.Add("Pending")
Dim col5 As GridViewComboBoxColumn = RadGridView1.Columns("column2")
col5.DataSource = lstValues
col5.DropDownStyle = Telerik.WinControls.RadDropDownStyle.DropDown
Now the row being added would be following.
RadGridView1.Rows.Add("Application Name", "Processing")
As you can see that column2 has no item named "Processing" so it is not displayed and showing as blank.
Thanks in advance for the help.
Regards
You can use the formatting events and set the cell text to whatever you need, however, note that the cell value will not be changed by this, but this is what you need as far as I can understand.
void radGridView1_CellFormatting(object sender, Telerik.WinControls.UI.CellFormattingEventArgs e)
{
if (e.Column.Name == "column2" && e.Row.Cells["someColumn"].Value == something)
{
e.CellElement.Text = "some text";
}
else
{
e.CellElement.ResetValue(LightVisualElement.TextProperty, ValueResetFlags.Local);
}
}
Related
I have a User Control with an Ultragrid. In a particular form where I add a ValueList. The value list will not show for the particular column I'm interested in. If I then code in another column instead by changing the index value for columns I get the value list in the column.
The code looks like:
private void AddCombo(object sender, UcUltraGen.RowClickArgs e)
{
ValueList vl;
if (!ucUltraGridMain.Grid.DisplayLayout.ValueLists.Exists("Texas"))
{
vl = ucUltraGridMain.Grid.DisplayLayout.ValueLists.Add("Texas");
}
else
{
vl = ucUltraGridMain.Grid.DisplayLayout.ValueLists["Texas"];
}
var row = e.VariantRow;
List<PcBase> list = PcBase.PcBaseList.Where(x => x.VariantId == row.Cells["Id"].Text).ToList();
AddValueList(list, vl);
ucUltraGridMain.Grid.DisplayLayout.Bands[0].Columns[1].ValueList =
ucUltraGridMain.Grid.DisplayLayout.ValueLists["Texas"];
And if I change to
...
ucUltraGridMain.Grid.DisplayLayout.Bands[0].Columns[2]
It works. How can I have changed the behavior of columns[1]?
The property in column[1] was read only that is it only had a "get" implemented. By adding a set it worked. Hope this helps someone.
I have a grid with 4 columns 'Id' 'Name' ' Address' 'Age'
I getting all the Id's in the list and binding to the grid. Now in Grid_itemDataBound event depending upon the Id I am getting the Name, Address Age for that particular row.
now I want to sort the grid depending on the Name.
How I can Sort depending on the Name which was binded to grid in Item databound
Please do Needful
my Code
grdEmployee.DataSource = lstEmployee.OrderBy(x => x.Id).ToList();
protected void grdEmployee_ItemDataBound(object sender, EventArgs e)
{
Label lblname = (Label)e.Row.FindControl("lblName");
Label lblAge = (Label)e.Row.FindControl("lblAge");
Label lblId = (Label)e.Row.FindControl("lblId");
lblName.Text = GetName(lblId.Text);
lblAge.Text = GetAge(lblId.Text);
}
Note: this is sample code I have manually written in this editor. please Ignore faults in the code and please understand my scenario.
I think, it would be better if you get all the data at once, then doing it in parts in each call to ItemDataBound. This way instead of just a list of ID you will have a DataTable or a List of Employees. And then you can sort this DataTable or List the way you like - using DefaultView.Sort (for DataTable) or LINQ OrderBy.
Is it possible, to bind (or similar) a standard textbox to display the contents (dynamically) of the selected cell within a datagridview textboxcolumn?
My goal is that when a cell within this column has it's value altered, the textbox.text is also changed, and when the user selects a cell then types something in this separate textbox the value is updating the datagridview textboxcolumns value on the fly.
Yes you may bind common dataSource to both TextBox and DataGridView.
public class Foo
{
public string Item { get; set; }
}
private void Form2_Load(object sender, EventArgs e)
{
List<Foo> list = new List<Foo>()
{
new Foo() { Item="1" },
new Foo() { Item="2" }
};
dataGridView1.DataSource = list;
textBox1.DataBindings.Add("Text", list, "Item");
}
Applying the idea from "adatapost" to a DataGridView in C#:
TextBox txt = this.txtBox1;
DataGridView dgv = this.datagridview1;
txt.DataBindings.Add("Text", dgv.DataSource, "FieldName");
dgv = null;
txt = null;
It might not be necessary to declare separate variables to hold your TextBox and DataGridView while you create the Binding. I just show these for clarity, in fact, this would be sufficient:
this.txtBox1.DataBindings.Add("Text", this.datagridview1.DataSource, "FieldName"
To clarify, "Text" means output the value back to the "Text" property of the TextBox, and "FieldName" should be replaced with whatever the column is in your DataGridView that you want to bind to.
Note - make sure that you have already set the DataSource / populated the DataGridView before you put the Binding code, if the DataSource is not set and therefore containing the field that you wish to bind to you will get an error.
I have two comboBox cb_Brand and cb_Model on a winForm.
cb_Model populates values on brand Select.
the problem is: if we select the brand any and select the any model under that brand, cb_Model does not loose the value of previous model selected.
for example: If we select the brand Audi and model A3
and the select the Brand Ford, when I click on cb_Model to select the model, it displayed the A3 as selected model, but still other models in list are belong to ford.
my code is:
private void cb_Brand_SelectedIndexChanged(object sender, EventArgs e)
{
// Clear Current Data
cb_Model.Text = "";
cb_Model.Items.Clear();
CarModel _carmodel = new CarModel ();
// Get Selected Car Brnad
int CarBrandID = _carmodel .GetCarBrandID(cb_Brand.Text);
//Enable choice of Model
SortedList<int, Model> colM;
colM = Model.ReadModel(CarBrandID);
cb_Model.DisplayMember = "ModelText";
foreach (Model objM in colM.Values)
{
cb_Model.Items.Add(objM);
}
}
Any Idea Please..
Thanks
unable to find the reason but sorted out with a temp fix:
private void cb_Model_Click(object sender, EventArgs e)
{
cb_Model.Text = "";
}
Thanks a lot guys
cheers
Instead of adding the items manually like this:
foreach (Model objM in colM.Values)
{
cb_Model.Items.Add(objM);
}
Let .NET take care of it for you and replace it with this:
cb_Model.DataSource = colMValues;
Which will bind the data to the list and refreshes the comboboxes items automatcially when a data source is set.
You will also not need these lines anymore:
// Clear Current Data
cb_Model.Text = "";
cb_Model.Items.Clear();
Have a read of this for more info on binding lists (and other data sources) to ComboBoxes:
How to: Bind a Windows Forms ComboBox or ListBox Control to Data (MSDN)
#w69rdy suggests an excellent solution.
The reason cb_Model did not change it's value is because you never changed the value. cb_Model.Items.Clear() does not change the selected index; only the items are removed from the combo box.
Using the code sample provided in your question:
// Clear Current Data
cb_Model.Text = "";
cb_Model.Items.Clear();
cb_Model.SelectedIndex = -1; // would effectively clear the previously selected value.
i had same problem now and Combobox's ResetText method solved the problem for me
This would work
combobox.ResetText();
I've tried your example. For me it worked as it should have.
You could try setting the cb_model.SelectedText to "" or SelectedItem to null
I found that keeping the scope of the data source near the loading of the combo box worked for me. I had a datatable with class level scope and it did not clear but then I brought it into function level scope and had it clear after the load and this worked.
I have a similar problem,tried cmb.resettext it clears text but not value.In my load form I have the below code:
Dim cmd As New SqlCommand("SELECT stud_id,name FROM student_details WHERE stud_id NOT IN (SELECT stud_id FROM student_details WHERE hostel_id!=0)", sqlcont.Conn)
Dim dr As SqlDataReader = cmd.ExecuteReader
Dim dat As New DataTable
Dim j As Integer
For j = 0 To dat.Rows.Count - 1
dr.Read()
Next
dat.Load(dr)
cmbstud.DisplayMember = "name"
cmbstud.ValueMember = "stud_id"
cmbstud.DataSource = New BindingSource(dat, Nothing)
dr.Close()
In my btnhostel click event I have the below code:
frmallocateHostel_Load(Nothing, Nothing)
this I put in attempt to reload my dataset and thus my comboboxes.Using cmbstud.resettext simply clears the text not the value.
I have same problem then I used
combobox1.SelectedIndex=-1
and it works.
I have a DataGridView with a Binding-Source bound to a Data-Table loaded from an Oracle-Database. (btw. I don't think that the Database-Connection could cause this)
I have also a DataGridViewComboBoxColumn bound to an class of persons (only "has" ID and name), so I can display / allow to edit the name instead of the ID. My problem is now that after the data-binding is completed c# automatically "selects" the first cell of the DGV - see the attached picture.
I also have to use this little piece of code to ensure data integrity:
private void _table_ColumnChanging(object sender, DataColumnChangeEventArgs e)
{
if (e.Column == _table.Columns["NEEDED_ID"])
{
if (e.ProposedValue == null)
{
e.ProposedValue = System.DBNull.Value;
}
}
}
Now using this _table.GetChanges() always returns the first row of the DGV as "modified" but the value isn't really changed - it's only DBNull instead of null. Can I somehow avoid automatic selection of first cell or how to avoid this behavior?
EDIT: Meanwhile I found out that changing the first column to something not editable fixes this problem. But this is nothing more than an workaround. I would really appreciate to get an working solution for this.
EDIT 2: I have also an empty Object 'on top' of the ComboBox-DataSource
DtoPerson blankPerson = new DtoPerson();
blankPerson.Name = String.Empty;
blankPerson.PersonRollenId = -1;
personList.Add(blankPerson);
Try to put a null in your DataGridViewComboBoxColumn's DataSource. example:
SELECT id, description
FROM yourTable
UNION
SELECT NULL, NULL
GridViewName.ClearSelection();