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;
}
Related
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();
}
}
I created a DataGrid. There are informations from database. i want delete selected row from datagrid and from database with button. i wrote these codes, program is running but when i click delete button there are Error "InvalidOperationException was unhandled". And what i must write my xaml file in datagrid tag ?
private void button3_Click(object sender, RoutedEventArgs e)
{
var selectedItem = dataGrid1.SelectedItem;
if (selectedItem != null)
{
dataGrid1.Items.Remove(selectedItem);
}
}
Try to delete by using row number.
dataGrid1.Rows.RemoveAt(dataGrid1.SelectedIndex);
Hope that works.
In the example below, when i try to set values to the combobox, the event is triggered and change objRunSettings.xxx before the value is set to cmbxxx.SelectedValue.
I suppose i need something like SelectionChangedCommited instead of SelectionChanged but i am really confused how to do it, as in xaml my only option is SelectionChanged
In xaml
<ComboBox SelectionChanged ="cmbxxx_selectionChanged"/>
In .cs -> set combobox values
cmbxxx.SelectedValue = objRunSettings.xxx;
Event
private void cmbxxx_selectionChanged(object sender, SelectionChangedEventArgs e)
{
objRunSettings.xxx = cmbxxx.SelectedValue.ToString();
}
The first item in the e.AddedItems IList will be the value you are looking for.
MSDN SelectionChangedEventArgs
I have a Datagrid with some rows in it and I want to select corresponding rows, when a user selects a row. When I do this in the SelectionChanged-Event, the rows get selected (dataGrid.SelectedItems is set correctly), but this is not displayed in the GUI (only the row header is selected - not the row).
If I select the additional rows in the SelectedCellsChanged-Event, everything works as expected. Does anybody know, what is happening in between? Or better: What should be the correct way to do this?
Here is some code to reproduce (BTW: using .Net 4.5.1):
<DataGrid SelectionChanged="DataGrid_SelectionChanged" Name="myDatagrid"
ItemsSource="{Binding}" SelectedCellsChanged="myDatagrid_SelectedCellsChanged"/>
private void DataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
// Does not display correctly
// myDatagrid.SelectedItems.Add(somerow)
}
private void myDatagrid_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
{
// Does display correctly
// myDatagrid.SelectedItems.Add(somerow)
}
I am trying to create a datagridComboBoxcolumn in winforms with Suggestions Based on Loose Character Search similar to Easycomplete combobox. but I want this as Datagridview combobox.
I have created a grid with Datagridviewcombobox column and used autocomplete but it will search only from first characters. I want loose search. I used
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (e.Control is DataGridViewComboBoxEditingControl)
{
((ComboBox)e.Control).DropDownStyle = ComboBoxStyle.DropDown;
((ComboBox)e.Control).AutoCompleteSource = AutoCompleteSource.ListItems;
((ComboBox)e.Control).AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.Suggest;
}
}
Please provide me a solution to create this type of datagridviewcombobox.
For that you have to create a custom DataGridiVew control. This is not a one line code or not a single class code. you have to make several classes for that.
public class MyDgv : DataGridView
{
....
}
And also create some classes like DataGridViewComboBoxColumn, DataGridViewComboBoxCell, DataGridViewEditingComboBoxControl
There is a tutorial how to create custom column in datagridview on msdn