Remove column from child gridview - c#

The parent of this grid is Project while the child is BOM. I've manage to display child grid using the following code.
private void gridView_MasterRowEmpty(object sender, DevExpress.XtraGrid.Views.Grid.MasterRowEmptyEventArgs e)
{
Project projects = (Project)gridView.GetRow(e.RowHandle);
e.IsEmpty = projects.BOMs.Count == 0;
}
private void gridView_MasterRowGetRelationCount(object sender, DevExpress.XtraGrid.Views.Grid.MasterRowGetRelationCountEventArgs e)
{
e.RelationCount = 1;
}
private void gridView_MasterRowGetRelationName(object sender, DevExpress.XtraGrid.Views.Grid.MasterRowGetRelationNameEventArgs e)
{
e.RelationName = "BOMs";
}
private void gridView_MasterRowGetChildList(object sender, DevExpress.XtraGrid.Views.Grid.MasterRowGetChildListEventArgs e)
{
Project projects = (Project)gridView.GetRow(e.RowHandle);
e.ChildList = new BindingSource(projects, "BOMs");
}
However, the code is showing me all the columns. I would like to hide some columns from the child which is BOM.
The only way I found was to use something like below
dataGridView1.Columns[index].Visible = false;
But where should I place the above code?
Updated Code
private void gridView_MasterRowExpanded(object sender, DevExpress.XtraGrid.Views.Grid.CustomMasterRowEventArgs e)
{
GridView gridView = sender as GridView;
GridView detailView = (GridView)gridView.GetDetailView(e.RowHandle, e.RelationIndex);
detailView.Columns["Column Name"].Visible = false;
}

To fulfill your need you must handle the Grid_MasterRowExpanded method in your GridControl :
private void Grid_MasterRowExpanded(System.Object sender, DevExpress.XtraGrid.Views.Grid.CustomMasterRowEventArgs e)
{
GridView view = sender;
GridView detail = view.GetDetailView(e.RowHandle, e.RelationIndex);
if (e.RowHandle == 0 | e.RowHandle == 1) {
if (detail.Name == "BOM") {
detail.Columns["Column Name"].Visible = false;
}
}
}

FYI, you can also do this at design-time, which is my favorite way to go. Within the gridView designer, you need to make sure you create a gridView for the master and child. Clicking "Retrieve Details" will do this. It will also blow away any columns you have already created, along with the layout, but the easy way around this is to save the layout as an XML, click "Retrieve Details" and then re-load the XML.
Here is what your designer will look like after you do that:
From here, you can click on each gridView (the master and child separately) and customize each -- change column widths, hide columns, DELETE columns (the data is still there but impossible for the UI to add to the grid), rearrange columns, etc.
And the best part -- no code.

Related

Hide column in datagrid WPF

I have a table in a database which I want to display in a WPF DataGrid. However I want to hide the first column. This column defines Id's for all items. I need the Id's for further actions, but I don't want to show it in the DataGrid. I've tried the code below, but I do get an error, on the last line, that the index has to be bigger than 0.
DbMainTable.ItemsSource = dataHandler.visibleDatabaseTable.DefaultView;
DbMainTable.Columns[0].Visibility = Visibility.Hidden;
If anyone can help me, let me know.
The problem is that when you are trying to set the visibility of the column it does not exist yet.
Try this:
In constructor:
DbMainTable.ItemsSource = dataHandler.visibleDatabaseTable.DefaultView;
DbMainTable.AutoGeneratedColumns += DbMainTable_OnAutoGeneratedColumns;
below:
private void DbMainTable_OnAutoGeneratedColumns(object? sender, EventArgs e)
{
DbMainTable.AutoGeneratedColumns -= DbMainTable_OnAutoGeneratedColumns;
DbMainTable.Columns[0].Visibility = Visibility.Hidden;
}
Could you provide more information about this issue? It is hard to guess what part of code is not working based on this.
But if I had to guess you use automatically generated columns and you try to hide this column before it is added to array of columns.
I event tried to do this with autogenerated columns and it gives me the same exception as you get.
To resolve this move this part of code somewhere where this datagrid (and its columns) is already loaded - for example to OnLoaded event handler in code behind
To achieve this:
in code behind add this method
private void DbMainTable_OnLoaded(object sender, RoutedEventArgs e)
{
DbMainTable.ItemsSource = dataHandler.visibleDatabaseTable.DefaultView;
DbMainTable.Columns[0].Visibility = Visibility.Hidden;
}
and in XAML:
<DataGrid x:Name="DbMainTable"
Loaded="DbMainTable_OnLoaded"
Grid.Column="0" />
If you need the id but don't want to see the column in the grid then I would think the simplest approach is not to add the column in the first place.
Work with the data which is in visibleDatabaseTable.
private void DbMainTable_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
if ((string)e.Column.Header == "Id")
{
e.Cancel = true;
}
}
On your datagrid:
<Datagrid .....
AutoGeneratingColumn="DbMainTable_AutoGeneratingColumn"/>

How to dynamically filter DataGridView using TextBox data?

I created a simple DataGridViewa with a single column and added a TextBox above.
Currently the text actually a DataTable (I though this would make things easier for filtering) with 2 columns, number and text (I hide the number in the DataGridView ). I can change it to any other class if required.
When the user enters a letter in the TextBox, I want to dynamically filter and show only the lines containing this text.
I load the data to the DataGridView like this:
private void PhrasesForm_Load(object sender, EventArgs e)
{
phrasesDataGridView.ReadOnly = true;
phrasesDataGridView.DataSource = _phrases.Phrases;
phrasesDataGridView.Columns[0].Visible = false;
this.phrasesDataGridView.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
}
When add another letter the filter will be readjusted.
What do I write here...
private void filterBox_TextChanged(object sender, EventArgs e)
{
}
If you have a DataGridView, or any Control with a DataSource, consider using Nuget Package BindingListView. It has functionality to sort the binding list by any column on a mouse click, but it also has functionality to filter the data.
List<Customer> customers = ...
BindingListView<Customer> customerView = new BindingListView<Customer>(customers);
dataGridView1.DataSource = customerView;
And presto, if shows all Customers. The columns depend on you column definition.
To filter the customers, for instance, show only customers with born before a certain date:
void ShowCustomers(DateTime limitDate)
{
customerView.ApplyFilter( customer => customer.BirthDay < limitDate));
}
Result: only the older Customers are shown.
You can do it similarly:
void ShowItemsWithPhraseStart(string phraseStart)
{
myView.ApplyFilter(row => row.Phrase.StartsWith(phraseStart));
}
And bingo: the datagrid shows only the row with a value for property Phrase that starts with phraseStart.
private void OnTextBocChanged(object sender, ...)
{
var text = this.TextBox1.Text;
ShowItemsWithPhraseStart(text);
}

Disable Excel-Like filtering for Some columns - Telerik C# Winforms

By enabling Excel-Like filtering for RadGridView all of the columns will have a filter button. I need to disable Excel-Like filtering for some specific columns and hiding the excel filtering button for that column. Is this possible?
I just found the solution using RadControlSpy, for hiding a excel-like filtering button I should manage ViewCellFormatting event like this:
private void gridShop_ViewCellFormatting(object sender, CellFormattingEventArgs e)
{
if (e.CellElement is GridHeaderCellElement && e.Column.Name == "IDCol")
(e.CellElement as GridHeaderCellElement).FilterButton.Visibility = ElementVisibility.Collapsed;
}
Try to access children of RadGridView and change Visibility property to Hidden :
int columnIndex = 1;
((Telerik.WinControls.UI.GridFilterButtonElement)(this.radGridView1.GetChildAt(0).GetChildAt(0).GetChildAt(2).GetChildAt(0).GetChildAt(0).GetChildAt(2).GetChildAt(1).GetChildAt(columnIndex ).GetChildAt(0))).Visibility = Telerik.WinControls.ElementVisibility.Hidden;
Tested on : Telerik 2015, visual studio 2013
Remember : do NOT use this command before initializing Form/GridView like constructor.
for example i use it on form load:
private void RadForm1_Load(object sender, EventArgs e)
{
int columnIndex = 1;
((Telerik.WinControls.UI.GridFilterButtonElement)(this.radGridView1.GetChildAt(0).GetChildAt(0).GetChildAt(2).GetChildAt(0).GetChildAt(0).GetChildAt(2).GetChildAt(1).GetChildAt(columnIndex).GetChildAt(0))).Visibility = Telerik.WinControls.ElementVisibility.Hidden;
}
In the current version, there is a column property, AllowFiltering, that you can set to true or false.
this.RadGridView1.Columns["MeetingNumber"].AllowFiltering = false;
https://docs.telerik.com/devtools/winforms/gridview/filtering/excel-like-filtering
Scroll down to "Enabling Excel-like filtering" for a description

Getting the selected row from dataGridView1

I have a dataGridView1 on my Wondows Form Application. I have populated the dataGridView1 with a list of strings. This all works great. But...
I need to be able to grab the row the user is selected on and I can't seem to do it. My code just gives me an error, written below.
I have copied GridViewRow form the msdn site.
private void editButton_Click(object sender, EventArgs e) {
GridViewRow row = dataGridView1.SelectedRow;
//This is what I have been using before with a list box.
if (itemList.SelectedIndex >= 0) {
int newInt = itemList.SelectedIndex;
Form form = new NewItemW(selectedFolder, this, items[newInt], WindowEnum.ItemEdit);
form.Show();
}
}
I have tried this but I am getting the error: "The type or namespace 'GridViewRow' could not be found."
My basic question is how do I get this to work?
Assuming you are using a Winforms DataGridView you should do it like this:
private void editButton_Click(object sender, EventArgs e)
{
if (DataGridView1.SelectedRows.Count > 0)
{
DataGridViewRow row = dataGridView1.SelectedRows[0];
if (itemList.SelectedIndex >= 0)
{
int newInt = itemList.SelectedIndex;
Form form = new NewItemW(
selectedFolder, this, items[newInt], WindowEnum.ItemEdit);
form.Show();
}
}
}
By copying from that link you have mixed the WPF Controls and their code with Winforms. Often folks mix up just the names DataGridView vs GridViewRow, but you have also copied the wong Property SelectedRow. Instead use the first element of the SelectedRows collection. Also always check or else the array acess will crash..
you are using the web control System.Web.UI.WebControls.WebControl.GridViewRow, it must be hosted in a web page.
For windows form use the System.Windows.Forms.DataGridViewRow control.
http://msdn.microsoft.com/en-us/library/vstudio/system.windows.forms.datagridviewrow(v=vs.110)

Updating textbox based on datagrid view

I have this datagrid view which links to the table answers in the database. The user can edit answers to questions in this form, but I'd like for the textbox to be updated as the button moves onto next answer. This is so the user can edit/delete stuff in the textbox and save it.
private void NextQuestion_Click(object sender, EventArgs e)
{
QuestionsBindingSource.MoveNext();
}
How can I get the textbox to refresh based on selected record in datagridview?
Since you are using a BindingSource, you can get the Current object, cast it to it's type, and get a value.
Let's assume you are bound to a DataTable:
private void NextQuestion_Click(object sender, EventArgs e)
{
if (QuestionsBindingSource != null)
{
QuestionsBindingSource.MoveNext();
if (QuestionsBindingSource.Current != null)
{
DataRow row = (DataRow)QuestionBindingSource.Current;
yourTextBox.Text = row["FieldYouWant"].ToString();
}
}
}
What you cast Current to and the subsequent value reference are both dependent upon what you are bound to (what QuestionsBindingSource is). Adjust this example accordingly.

Categories