the event is triggered when the Form with the DataGridView is initialized, when the column headers texts are placed. So the function handling the event runs at start-up as many time as there are columns. How can I stop that and have it triggered only after the user made some changes in the datagrid?
Added:
It seems that changing from CellValueChanged to CellEndEdit does the trick.
Have you tried using the SelectionChanged property of the datagrid, triggering an event when the content changes?
<Grid>
<DataGrid> Name="TestDataGrid" SelectionChanged="DataGrid_EventName" />
</Grid>
Within the Code-Behind, the event will be captured:
private void DataGrid_EventName(object sender, SelectionChangedEventArgs e)
{
//Conduct work here
}
Edit: I'm quite sure that you will still need to initialize the DataGrid.
For example, you can use this event to extract the row-specific information from within the DataGrid:
private void DataGrid_EventName(object sender, SelectionChangedEventArgs e)
{
DataGrid _dataGrid = (DataGrid)sender;
DataRowView selectedRow = _dataGrid.SelectedItem as DataRowView;
if (selectedRow != null)
{
RowItemList = new List<string>(); //List declared outside of this method...
//add row information to the list
RowItemList.Add(selectedRow.ToString());
//or, get column-specific information
string ColmnEntry = selectedRow[ColumnName].ToString();
}
}
Related
I'm trying to call a method which fills a table with data according to the ListBoxItem that has been selected.
// Setting the ListBoxItems
myListBox.ItemsSource = list;
// Calling the method when the ListBox's selection changes
myListBox.SelectionChanged += LbItem_Select;
The above snippet can't work because the LbItem_Select event handler doesn't get as a parameter the item that is currently selected.
This is the event handler:
private void LbItem_Select(object sender, RoutedEventArgs e)
{
var lbItem = sender as ListBoxItem;
lbItemContent = lbitem.Content.ToString();
// fill the table according to the value of lbItemContent
}
How could I achieve this?
When you work with events, sender is the object related to the event. myListBox.SelectionChanged is an event of the ListBox. So sender is the ListBox, not the item. Try with this:
private void LbItem_Select(object sender, RoutedEventArgs e)
{
var listBox = sender as ListBox;
// Or use myListBox directly if you have the ListBox available here
var item = listBox.SelectedItem;
// Do whatever with the item
}
I am programming an interactive datagridview on WPF, the user can click on cells causing the content to change via selecting from multiple data sources. When the user click a cell the double click cell event is triggered like:
private TabItem item(..){
Table= new forms.DataGridView();
Table.DataSource = DataSource1(); //Default
Table.CellDoubleClick += delegate (object sender, forms.DataGridViewCellEventArgs args){
{
var senderGrid = (forms.DataGridView)sender;
//Depending on some conditions the user can switch to DataSource2(), DataSource3()
if(Condition 1)
senderGrid.DataSource = DataSource2();
}
Up until this point, everything works as intended ! The table double click event switches data sources as programmed.
Now the issue occurs, when I want to update the table from a Combo Box event trigger. The table remains simply unchanged, I guess I am not addressing the object correctly.
//Still inside the same TabItem item(..)
newCombo.DropDownClosed += delegate (Object sender, EventArgs e)
{
ComboBox cmb = sender as ComboBox;
if(Condition)
Table.DataSource = DataSource2();
}
Is there a way to control the table outside its event functions ?
Hello stackoverflow community,
i am learning c# gui datagrid part
so i created a datagrid and added two column headers as shown below:
Now i have this value of a class
Program.AllStudents[Form1.userId].name;
Also i clicked on the datagrid and this function appeared
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
But I want my changes to appear once the form of datagrid is loaded not onclick
i want to add it for the name column,what should i do?
also the columns are editable so i want to know how to access them later
Thanks
You can use the DataGrid 'loaded' event like this:
xaml:
<DataGrid Name="dataGridView1"
Loaded="dataGridView1_OnLoaded"
>
c#:
private void dataGridView1_OnLoaded(object sender, RoutedEventArgs e)
{
dataGridView1.ItemsSource = Program.AllStudents[Form1.userId].name;
}
I have a loaded event of combobox placed in each datagrid row.Event is as following.
private void show(object sender, RoutedEventArgs e)
{
ComboBox cmb = null;
if (sender is ComboBox)
{
cmb = (sender as ComboBox);
}
for (var vis = sender as Visual; vis != null; vis = VisualTreeHelper.GetParent(vis) as Visual)
if (vis is DataGridRow)
{
var row = (DataGridRow)vis;
break;
}
cmb.items.add("1","2","3");
}
Now I want to call it at winload or any button event as method. how it is possible.
I am sorry, but what you're saying is not possible. You cannot find a combobox loaded event from somewhere, since it is not under your control.
It is fired internally when the combobox is loaded. Hence you get the sender as combobox.
If your purpose is simply adding values to the combobox consider binding the data template of your data grid to some source and bind the combobox source to some collection. Doing that will add the items initially to the combobox while the application is loaded and you could change the values accordingly while the application is running by changing the bound sources of combobox in the datatemplate.
I am using VS2005 (c#.net desktop application). When I add an eventHandler to a combo of datagridview, it's automatically adding the same eventhandler to all other combos of the same datagridview.
My code:
private void dgvtstestdetail_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
DataGridView grid = (sender as DataGridView);
if (grid.CurrentCell.OwningColumn == grid.Columns["gdvtstd_TestParameter"])
{
ComboBox cb = (e.Control as ComboBox);
cb.SelectedIndexChanged -= new EventHandler(dvgCombo_SelectedIndexChanged);
cb.SelectedIndexChanged += new EventHandler(dvgCombo_SelectedIndexChanged);
}
}
I want to add different event handlers to different combos in datagridview. Please tell me how can I do it.
By default, I believe that the DataGridColumn reuses the same instance of ComboBox for each cell. This is an optimization used by the grid to keep the number of created editing controls low.
The easiest thing is just to have one event handler, check the cell being edited, and take the appropriate action.
public void dvgCombo_SelectedIndexedChanged()
{
if (<condition1>)
ExecuteConditionOneLogic();
if (<condition2>)
ExecuteConditionTwoLogic();
}
A more advanced solution would be to create your on custom DataGridViewColumn implementation which does not share an editing control. I wouldn't recommend this unless you really have some reusable functionality.