C
C#2y ago
SCOLANATOR

❔ Problem populating Datagrid

I'm trying to populate a datagrid where certain columns are populated with information that is read only, with the other columns open for a user to input data and with one column with a checkbox binary option. I have been struggling to get this code to work and would appreciate some help. I'm very new to WPF and C# (done a lot of VB6 and PoSh, some C, some Python). Please let me know how I should post my code.
19 Replies
Buddy
Buddy2y ago
$details
MODiX
MODiX2y ago
When you ask a question, make sure you include as much detail as possible. Such as code, the issue you are facing, and what you expect the result to be. Upload code here https://paste.mod.gg/ (see $code for more information on how to paste your code)
SCOLANATOR
SCOLANATOROP2y ago
BlazeBin - uirbfxijpapy
A tool for sharing your source code with the world!
MODiX
MODiX2y ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat If your code is too long, post it to: https://paste.mod.gg/
SCOLANATOR
SCOLANATOROP2y ago
These are the errors that are popping up
Klarth
Klarth2y ago
tax_election_grid.DataSource = data; I don't understand why you're using this array as if it's a DataGrid? DataGridViewCheckBoxColumn chkBoxColmn = new DataGridViewCheckBoxColumn(); for this type not being found, you can ctrl+. in VS (or click the light bulb) when the cursor is on top of the red squigglies. VS should suggest to automatically add the using statement for the necessary namespace.
SCOLANATOR
SCOLANATOROP2y ago
Thanks Klarth, I pulled this from somewhere and modified it for my needs. So I'm not sure why it's done the way it is, I just started with C# so I'm trying to get to grips with it. So for datagridviewcheckboxcolumn should I create a new private class?
Klarth
Klarth2y ago
No.
Klarth
Klarth2y ago
This error means that the correct namespace hasn't been referenced, so the compiler cannot resolve the type.
SCOLANATOR
SCOLANATOROP2y ago
Is there a better example I can use to populate datagrid? I'm absolutely pulling my hair out trying to get this to work. I feel like it shouldn't be this difficult to populate a data grid with an array.
Klarth
Klarth2y ago
Now that I look at it, I think you're following an example from WinForm's DataGridView and not WPF's DataGrid.
Klarth
Klarth2y ago
Klarth
Klarth2y ago
See how it suggests the WinForms namespace. You should probably start by replicating https://wpf-tutorial.com/datagrid-control/introduction/ As far as having check-able state, you're going to hit a wall pretty fast because you likely don't understand data binding or INotifyPropertyChanged yet. Ah, autogeneration takes care of one half of that equation. You still want data binding / INotifyPropertyChanged for when you change state in code-behind though.
Klarth
Klarth2y ago
Klarth
Klarth2y ago
https://paste.mod.gg/jjbpmfuzqubn/0 and https://paste.mod.gg/yppjuapszhpr/0 Only code-behind. If you modify the users in code-behind, it won't automatically reflect in the GUI because the correct abstractions (ObservableCollection<T> and INPC) weren't used.
SCOLANATOR
SCOLANATOROP2y ago
Thank you Klarth, that's a good explanation to work with. I have actually come across INotifyPropertyChanged but I haven't messed with it yet. i really just need to get these grids functioning properly and it should be fairly smooth sailing after that While I'm at it, is there a simple way to take an integer and use it to give a datagrid that many rows? That guide you gave me really helps
Klarth
Klarth2y ago
That's not really something you should do. You can create N empty objects (eg. Users) and add them the source. For INotifyPropertyChanged, I would either: 1. grab an implementation from https://stevemonaco.github.io/2020/08/wpf-2020-part2-mvvm-overview/ (disclaimer: I wrote the article) or 2. read the article for a basic understanding and instead use CommunityToolkit.Mvvm from NuGet which provides significantly better implementations of most MVVM things you'd ever need. The docs are also pretty good.
SCOLANATOR
SCOLANATOROP2y ago
I'll read through these and get back to you if I have any follow up questions
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server