How to change cell value dynamically in ListView with GridView - c#

I have used the GridView inside the ListView. I want to dynamically change a cell value. For example, there is a column named "Time", I wanna to change this value when something happens. I can find and get the cell (e.g od.Time as in following codes), but can not change its value. Some codes like this where ObjectData is the class for all ListView columns.
for (int i = 0; i < 5; i++)
{
ObjectData od = (ObjectData)objectListView.Items[i];
if (od.UserName == "Tom")
{
od.Time = DateTime.Now.ToString();
}
}

If the your listview is bound to a data source you'll need to change the data source's data, not the listview's contents. It should then update the listview.

Related

WPF drop-down list for auto generated column in datagrid

How to make a drop-down list when you click on an element of a specific table column in which you can select an element for this cell? Column is auto generated.
A Combobox in xaml/wpf is used like this:
<ComboBox x:Name="some Name" SelectionChanged="comboboxChanged">
<ComboBoxItem>The Content of your Combobox</ComboBoxItem>
</Combobox>
ComboBoxItems are essentially the dropdown part. You can add as many as you want.
In your back end (c#) you can get the selected Value as soon as the "SelectionChanged"-event is triggered. The code for getting the selected Value can be done multiple ways. Example:
private void comboboxChanged(object sender, SelectionChangedEventArgs e){
string comboboxvalue = comboboxname.Text;
//Then set associated textblock or label
labelname.Content = comboboxvalue;
}
The code above would be static though. Dynamically generating these elements could look like this for instance.
When auto-generating, using an inline function for the event is easy.
for (int i = 0; i < 10; i++){
ComboBox comboboxname = new ComboBox();
comboboxname.SelectionChanged += (ss,ee) { string comboBoxValue = comboboxname.Text; labelname.Content = comboBoxValue;}
}
Labelname being the name of the Label you want to set. In that loop you will need to implement a way of giving each box a unique name and getting the name of the associated label in there as well. That you will have to figure out on your own as i do not know how and what exactly is generated and what is static.
You will also need to add your dynamically created combobox to your listpanel or grid or whatever you are using. This works like this:
listpanelname.Children.Add(comboboxname);
Just add that to the "for" loop.

How Change Color Row in GridView when I want Apply condition it Row in C# & WPF, how to do it?

How Change Color Row in Grid View when I want Apply condition it Row in C# & WPF, how to do it?
(Working in C# and WPF)
Note 1: columns Grid View is: Product ID, Product Name, ProductStartDate, Product Type and Product Price.
Note 2: cell index Zero in Grid View is Product ID, cell index One in Grid View is Product Name , cell index Two in Grid View is ProductStartDate , cell index Three in Grid View is Product Type, cell index four in Grid View is Product Price.
Note 3: Data Type Column Product Type in Grid View is string and value it is "Sale" value or "Buy" value.
My Code:
int gridviewrowcount = gridview1.items.count;
string type;
for(int i = 0; i <= gridviewrowcount-1 ; i++)
{
type = gridview1.columns[3].GetCellContent(gridview1.items[i]) as TextBlock;
}
This code Get gridview1.columns [3] text value in variable string type.
But, i need gridview1.columns [3] text value is "sale" Change Color Row it
in Grid View is Green color. And i need gridview1.columns [3] text value is
"Buy" Change Color Row it in Grid View is Red color?
(Working in C# and WPF)
Thank's in advance
To conditional formatting the GridView in WPF, you will need to handle the LoadingRow event.
Bellow I'm showing a sample code you can accomplish the result. I supposed that you are population the GridView with a IEnumerable<T> of some Model class, that I've called ProductModel, with a public property named ProductType.
You can put this code on the constructor of your Form / View class.
//Row Style.
gridview1.LoadingRow += (_sender, _e) =>
{
ProdutctModel item = (ProductModel)_e.Row.DataContext;
if (item != null)
{
if (item.ProductType.Equals("Buy"))
_e.Row.Foreground = new SolidColorBrush(Colors.Red);
else if (item.ProductType.Equals("Sale"))
_e.Row.Foreground = new SolidColorBrush(Colors.Green);
}
};
As you can see are simple, you just need change the Row.Foreground according to the Product Type, as you want.

Populate DataGridView ComboBoxCell programmatically

I have a DataGridView in winforms, that contains a combobox column. The gridview is filled programmatically, and it is empty at start. "list" is a list containing custom objects. Value1/2/3 are unique to each row.
int[] array = new[] {value1,value2,value3}
for (int i = 0; i < list.Count; i++)
{
DataRow newrow = MyDataTable.NewRow();
newrow["idColumn"] = list[i].Customer_id;
newrow["dateoforderColumn"] = list[i].Customer_date;
newrow["commColumn"] = list[i].Customer_comment;
// This is what I want, and can't get to work.
newrow["comboColumn"] = array;
// I have also tried (newrow["comboColumn"] as DataGridViewComboColumn).DataSource
// but it didn't work either.
MyDataTable.Rows.Add(newrow);
}
How can I populate a freshly created combobox cell? Also, I need to do it within the for loop, because I need the index to get the data.
From the look of your code you just have one array of values that you need to set. In this case you only need to set it once for the column, not for every row, i usually do this in the constructor or before populating rows.
//Clear any junk items that may have been there from the designer or previous view
((DataGridViewComboBoxColumn)MyDataTable.Columns["comboColumn"]).Items.Clear();
//Add you new values
((DataGridViewComboBoxColumn)MyDataTable.Columns["comboColumn"]).Items.AddRange(array);
Then to set the value of the new row's cell
//Set value to be selected in new row
newrow["comboColumn"] = list[i].Customer_value;

How to set max length of datagridview column

I have a DataGridView where the units can be entered in a TextBox column.
How do I restrict the input length of this column to 6 characters?
Use the MaxInputLength property of the DataGridViewTextBoxColumn.
This property is available through the Designer or through code:
((DataGridViewTextBoxColumn)dataGridView1.Columns[yourColumn]).MaxInputLength = 6;
Please use CellValueChanged event of DataGridView.
In the handler of the event you can check ColumnIndex and RowIndex properties of DataGridViewCellEventArgs argument to identify that grid's field of interest is edited and then - take appropriate actions.
As stated in other answers - most natural way to restrict text lengths for DataGridView field is to modify respective grid column properties. Properties of grid columns can be altered on Edit Columns form that is invoked for grid control in form designer with right click menu item Edit Columns...:
You may have to play with cell-edit events.
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellvaluechanged.aspx
You don't necessarily have the columns ready to manipulate if you're using data binding. For data binding, using the ColumnAdded listener can help:
public FormSingleValidation(BindingList<ValidateSingle> validateSingles)
{
InitializeComponent();
dataGridViewSingleValidation.ColumnAdded += ColumnAdded;
this.validateSingles = validateSingles;
var source = new BindingSource(validateSingles, null);
dataGridViewSingleValidation.DataSource = source;
}
private void ColumnAdded(object sender, DataGridViewColumnEventArgs e)
{
if(e.Column.GetType() == typeof(DataGridViewTextBoxColumn))
{
DataGridViewTextBoxColumn column = (DataGridViewTextBoxColumn) e.Column;
column.MaxInputLength = 6;
}
}
Caveats
Obviously this applies to all text columns without discrimination, you can add a conditional filter using the column's name if you only want specific columns to be effected.

Grid View Combo Box Column

I am working on a Windows Form.
I have a ComboBox and on it's SelectedIndexChanged, I need to bind values to a GridViewComboBox Column.
Suppose I have bound all the states in the ComboBox, then when a state is selected, I need to bind all the districts coming under the particular state.
How can I do it?
I am not sure of the question truly. Somewhere you wrote Combobox and sometimes GridViewComboBox. Still if you want what I am thinking:
datagridview dg;
string[] district = { "dis1", "dis2", "dis3" };
for (int i = 0; i < dg.Rows.Count; i++)
{
DataGridViewComboBoxCell cc = (DataGridViewComboBoxCell)dg["dgc", i];
cc.DataSource = country;
cc.Value = country[0];
}
dgc = Column name of DatagridViewComboboxColumn.
Here, putting the following code in State Combobox TextChanged event. String array district are district source. Tweak this concept according to your need, I think you will get what you are looking for.
Need any more clarity, add a comment.
Hope it helps.

Categories

Resources