LINQ Query is Data Source for DataGridView. How to control columns?

doni49

Member
Joined
Mar 18, 2014
Messages
14
Programming Experience
10+
How can I control the following aspects of my DataGridView columns?
  • Don't show certain columns (the object being queried includes ID values in multiple fields that I don't want to be displayed).
  • Set justification individually as three of the columns will show dollar amounts and I want them to be right justfied
  • Set formatting of the contents (the same three dollar amount columns should only show two decimal places).

Also two related questions. I'm writing what is basically an electronic checkbook.

1) I need to show one field (transaction amount) in one of two columns:
  • if the field is negative, show it in the withdrawal column
  • if the field is positive, show it in the deposit column

2) I need one column that doesn't correlate to a field in the query. This will be for a running balance and I want to have a function that will add/subtract each transaction from previous row's running balance value. Can I do this? If so, can you offer any pointers?

TIA!
 
Create your columns in the designer and set their DataPropertyName properties to tell them which properties of the data source to bind to. You would then set the AutoGenerateColumns property to false before binding, so those properties of the items that you don't want displayed will not be displayed. You can also add a column and not set its DataPropertyName to have an unbound column.

With regards to displaying data from one source property in two columns, simply use the same DataPropertyName value for both columns. I believe that you can handle the CellFormatting event of the grid to tell those columns to only render values in cells under certain circumstances, allowing you to show some data in one column and some in the other.
 
Create your columns in the designer and set their DataPropertyName properties to tell them which properties of the data source to bind to. You would then set the AutoGenerateColumns property to false before binding, so those properties of the items that you don't want displayed will not be displayed. You can also add a column and not set its DataPropertyName to have an unbound column.

With regards to displaying data from one source property in two columns, simply use the same DataPropertyName value for both columns. I believe that you can handle the CellFormatting event of the grid to tell those columns to only render values in cells under certain circumstances, allowing you to show some data in one column and some in the other.
I feel like I'm going in circles. I've seen lots of examples saying to "bind the data to the query". But I have yet to figure out how to do that via the designer. I have a function that reads the data file into memory at the end of that function, I created the query and then bound the DataGrid to the query. But of course that means it doesn't exist at design time so I can't do the bindinging (to the columns) at design time.
 
I have yet to figure out how to do that via the designer.

That's because you can't. I didn't say anything about binding in the designer. I said to create the columns in the designer and set their DataPropertyName properties to tell them which property of the data source items to bind to. You then set the DataSource in code at run time and the correct bound-item properties will be displayed in the correct columns of the grid.
 
Thanks. That makes sense. I was trying to set it via "Data Bindings" in the UI.

I was able to get it working. But there is another issue with it. In the attached screenshot, you'll see that all the columns are being displayed as Right Justified. But only the three columns on the far right are supposed to be right justified.

MyAppColumnJustification.png
 
You created the columns in the designer, you configure the columns in the designer. They have properties and they are set just like any other properties in the designer.
 
That's what I thought but I used the designer to set all the columns EXCEPT the three specified to be Middle Left. The three on the right are set (via the designer) to Middle Right.

Yet as you can see, they're ALL coming out as Right Justified.
 
The Payee column is one of the ones supposed to be left justified but isn't. You saw the first screenshot showing that it's right justified. Here's the column definition properties page in the designer.

MyAppPayeeColumn.png
 
Ok I found it. Even though I had configured the columns to show the alignment I need, I just found that there is a setting for RowTemplate>DefaultCellStyle which was set to middle right. I've changed that to Not Set and now it's letting the column have control.
 
Back
Top Bottom