VB.NET, How do I add a row to a datagrid in WinForms consisting of only checkboxes?

guest2013-1

guest
Joined
Aug 22, 2003
Messages
19,800
I'm busy with a small Importer I'm writing and would like the first row of a databound datagrid in a WinForms application written in .NET to be only checkboxes.

The idea behind this is, so that people can check the columns they want to import or contains the data they'd like to push through to the database.

If I can get this working, I'm also going to add a combobox at the very top with a list of columns in the database to which the data can correspond to.

For example, I have a CSV file I successfully selected and now displaying a preview of the data in a datagrid on a form. I'd like to be able to select which columns I want to import by checkbox or by combo box situated in the first row (all columns is same object, and object is specific to that column)

Once the selection is made, and the import button hit, I'll be able to only process the columns specified and push those through to the database.

I guess I could do with 1 row of only comboboxes as well and have an "ignore" option...

Any ideas would be helpful... currently it's piss adding a column of only select boxes, but I'd like to apply that to the entire row and have that row responsible for the columns being imported or not
 

Fuma

Executive Member
Joined
Jul 9, 2007
Messages
5,111
-Edit- Decided to start ignoring stupidity

I suppose you don't like the taste of your own medicine, do you?

If someoneelse asked the same question, you'll be the first person to say google and and. :p
Hypocrisy
 

guest2013-1

guest
Joined
Aug 22, 2003
Messages
19,800
I suppose you don't like the taste of your own medicine, do you?

If someoneelse asked the same question, you'll be the first person to say google and and. :p
Hypocrisy

Actually, most of the time I'd help them with Google phrases and/or links to the Google searches I've done that would help them finding the answer instead of spoon feeding them (like you expect us to do for you each time)

Ask anyone on here, I'm more than helpful most of the time to the posters on here, sure I have a ****ing attitude when it comes to these things, especially when wanna-be's pretend they can code when they can't think for themselves even... but you can't deny I have not helped. So I'm choosing to ignore your stupid assed comments.
 

zadbnguy

Well-Known Member
Joined
Aug 27, 2009
Messages
392
I suppose you don't like the taste of your own medicine, do you?

If someoneelse asked the same question, you'll be the first person to say google and and. :p
Hypocrisy

What is wrong with saying, "I don't know ... google is our friend?"
 

Fuma

Executive Member
Joined
Jul 9, 2007
Messages
5,111
Actually, most of the time I'd help them with Google phrases and/or links to the Google searches I've done that would help them finding the answer instead of spoon feeding them (like you expect us to do for you each time)

Ask anyone on here, I'm more than helpful most of the time to the posters on here, sure I have a ****ing attitude when it comes to these things, especially when wanna-be's pretend they can code when they can't think for themselves even... but you can't deny I have not helped. So I'm choosing to ignore your stupid assed comments.

Take it easy man. You are always right I know, wanna-be's like us who can't think for themselves are always wrong. Have a biscuit. :p
 

jeremie

Active Member
Joined
Mar 8, 2010
Messages
66
Is it possible if, instead of using checkboxes, you map the column index to the table column name?
 

guest2013-1

guest
Joined
Aug 22, 2003
Messages
19,800
Is it possible if, instead of using checkboxes, you map the column index to the table column name?

And that would get me where exactly?

You reckon I should try and dynamically create a bunch of controls instead of doing it via a grid?
 

jeremie

Active Member
Joined
Mar 8, 2010
Messages
66
What I see you trying to do is this:
There is a grid of data and you want some of those columns' data to be sent to a table as records right?
How many table columns are there on average say?

I don't understand the checkboxes part and why you need them.

Maybe this could help:

1.Have combo boxes that contain table columns names
2.Next to boxes have numeric drop down boxes, that's where the column name and datagrid column index will be "mapped"
3.When you insert data,then you have to create the insert statement

INSERT INTO <table>
VALUES (<value1>, <value2>, ..., <valueN>)

The values will be the data grid cells when you iterate through the datagrid rows, so value one will be the value of the cell number chosen for the first table column.

Not easy to explain it, and I know there are tools for this.
 

jeremie

Active Member
Joined
Mar 8, 2010
Messages
66
dynamically create a bunch of controls instead of doing it via a grid?
Yep, if you have tables with different column count then I would create dynamic grid.
The dynamic grid would have 2 columns and a row count equal to table column count.
In the Grid's columns, column 1 has all column names , column 2 can have numeric drop down boxes(not necessary coz you can type in column no of the grid on your win form)

You know there might be validation issues depending on your data, but like I said, there are tools for this that you can buy.
 
Last edited:

Necuno

Court Jester
Joined
Sep 27, 2005
Messages
58,567
I'm busy with a small Importer I'm writing and would like the first row of a databound datagrid in a WinForms application written in .NET to be only checkboxes.

The idea behind this is, so that people can check the columns they want to import or contains the data they'd like to push through to the database.

If I can get this working, I'm also going to add a combobox at the very top with a list of columns in the database to which the data can correspond to.

For example, I have a CSV file I successfully selected and now displaying a preview of the data in a datagrid on a form. I'd like to be able to select which columns I want to import by checkbox or by combo box situated in the first row (all columns is same object, and object is specific to that column)

Once the selection is made, and the import button hit, I'll be able to only process the columns specified and push those through to the database.

I guess I could do with 1 row of only comboboxes as well and have an "ignore" option...

Any ideas would be helpful... currently it's piss adding a column of only select boxes, but I'd like to apply that to the entire row and have that row responsible for the columns being imported or not

there is a way to add a custom header row with your label + checkbox or just checkboxes to a datagrid, but it is a bitch to upkeep as with each "redraw" you would had it reconstruct because it is dynamic.

getting values from it not so hard...

.../goes looking for example
 

Necuno

Court Jester
Joined
Sep 27, 2005
Messages
58,567
note this can also be used to have header row that has "two rows" as header where you would have in the one the main heading and in the second a sub heading like....

Code:
|       gender     |
|male  |     Female|


...using the RowCreated event

this is where you create and add the row. this must match your cells is. so if you have 10 databound you must create and add 10 cells here. also if you have more than one row it's added from last to first.

Code:
            if (e.Row.RowIndex != 0) return;

            //create header
            
            GridViewRow rowHdr1 = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Insert);

            GridViewRow rowHdr2 = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Insert);

            //set header row id's

            rowHdr1.ID = "gridHdr1";
            rowHdr1.ID = "gridHdr2";

            //add cells to header row 2
            //code to add cells, this can include controls and all other funky things :)

            //for example cell add
            foreach (TableCell cell in gvTheGrid.HeaderRow.Cells)
                   gridHdr2.Cells.Add(new TableCell());

            //for example control add
            TextBox txtBox = new TextBox();
            txtBox.MaxLength = 5;
            txtBox.Width = 40;
            txtBox.ID = "txtSearchCreatedBy";
            txtBox.Text = createdBy;
            gridHdr2.Cells[0].Controls.Add(txtBox);

            //add cells to header row 1
            //code to add cells

            //add header rows to grid

            gvTheGrid.Controls[0].Controls.AddAt(0, rowHdr2);
            gvTheGrid.Controls[0].Controls.AddAt(0, rowHdr1);


to get values on post


Code:
            //Get search row. note the textbox was added to the second header in the example ;)
            GridViewRow row = (gvTheGrid)gvSelectCourses.Controls[0].FindControl("gridHdr2");

            //it was added to the first cell.
            var txtBox = (SelectDate)row.Cells[0].FindControl("txtSearchCreatedBy");

            var createdBy = txtBox.Text


on net example
http://www.codeproject.com/KB/aspnet/Merge_Header.aspx
http://csharpdotnetfreak.blogspot.com/2008/11/merging-gridview-headers-to-have.html
http://www.dotnet247.com/247reference/msgs/13/69744.aspx
 
Last edited:

semaphore

Honorary Master
Joined
Nov 13, 2007
Messages
15,206
more importantly why the fk are you using VB :p


Let me understand this you have columns

Column1, Column2 , Column3

and in each you got data.

1,2,3

and in each column you want a control []1,[]2,[]3 ?:sick:

Well from my understand behind the principle of CSV files, its line imports not selective's so you could land up with data integrity loss. Also you are going to run into issues with controls if you have a few thousand lines, not to mention it will be a nightmare to use and click all those little checks. I will get back to you once i thought up something.
 

guest2013-1

guest
Joined
Aug 22, 2003
Messages
19,800
Thanks for the replies guys, will play around with it. And to me there's no real difference (except syntax) between VB.NET and C#... all the C++ losers are converting to .NET (for years now) and that's most probably why it's "preferred" *shrug*. If you show me evidence for one being better than the other (convincing evidence) then sweet. But anyway, don't want this to turn into a debate over this. VB.NET for me is just easier and faster to develop in, so if I don't have any deadlines I actually sit and go through C# which I can (for the most part) write anything in... just sometimes get confused re: syntax.

@Semaphore: RE: "Well from my understand behind the principle of CSV files, its line imports not selective's so you could land up with data integrity loss"

CSV's are the most popular form of exported database data (I stand corrected) ever. They don't carry (or need to carry) data integrity as they're formatted in such a way to have record upon record of data, even if it repeats. We're not using them as storage, we have databases (relational) for that, but when we want to export data into raw files (for example excel) we don't want to have:

1,22,Penis
2,22,Vagina
3,22,Sex
3,23,Tricycle

We want the following:

Adult Entertainment,Penis
Adult Entertainment,Vagina
Adult Entertainment,Sex
Kids Entertainment,Tricycle

That makes more sense to someone wanting to manipulate the data for (example) reporting purposes.

Now considering that those are the most popular means of exporting data, and that this data doesn't contain integrity at all (it's all in one line, nothing relational about it and in 99.99% of the cases, the code that exports these files faults if it ****s up the "integrity" of the comma-delimited file, for example not using the correct text-identifiers and a comma ****ing up everything else... which any import program should check for, like mine) and that you cannot control (most of the time) the way and the order you receive these CSV's in, you have to somehow make your program work... right? or do you tell your client "Sorry, it's impossible?"

I'm going to re-iterate the same thing I've told all my juniors I've ever had under me... You can't force the user to change their data, you need to build your application to compensate for what they currently have, especially when they have other legacy systems in place producing this data for them. As an example of why I say this, I worked for a publications company a few years back who had 4 flat-file databases circa 1990, nothing relational in there and even though they contained the same data (magazine subscriptions/contacts), each of them differed slightly depending on the "custom" programming that took place on each of these dBase databases. So one would export Col1,Col2,Col3 and the other would export Col2,Col1,Col3,Col***WeetJy

So, what I'm attempting is for them to specify which columns need to go into which fields in the database, so the preview grid should look something like this:

Column0 | Column1 | Column2
[ ] | [ ] | [ ] <-- this contains no data, only an indication of whether or not this column is chosen to actually go into "Step 2"
Data | My balls | Are blue
....only 5 rows of data needs to display really, it's the columns I'm interested in, not the data...

So they can then check which columns they want to import, and Bob is your uncle, the next step would be to take their columns they selected, and giving them comboboxes to choose from which database fields they should go into and from there it goes into a nice little relational database. All data-integrity-and-snug-with-coco-and-a-blanky. Of course I could actually only make it 1 step, and have comboboxes in there to begin with so they can choose and import within a few clicks, but I've learned before (from working with the high-caliber data capturers you get these days :sarcasm:) that it's more confusing to them the "easier" (I think) I made it to work with. So inserting an extra step sometimes is really best... even if it's not the quickest way to do things.

Now, I could tell the client, "Impossible, you have to change the ways you get your data to be able to use my super-duper program" OR I could actually create my program to save them the effort of converting these CSV's into the correct "structure" I require from them to import it successfully... as I've been down that road before when my boss suggested they'll just quickly contact all of their suppliers (they did cold calling etc as well) and MAKE them change the file formats they got... I'm still LOL'ing.... but in any case.

I also ALWAYS and FOREVER try to get my clients to piss off. I'm not a people person, I don't want to code this **** forever and I don't want to "hardcode" several pieces just to make **** work with their current data, something which might change 2 days/2 months/2 years down the line. In my heart I know if I give them the ability to choose which columns fit in where, they can import the data they receive much easier and give me less of a pain in the ass dealing with "your program is ****!!" (when it's the data structure that changed for the umpteenth time, like Vodacom's exports they do on prepaid vouchers and recharges) and a lot more "Me" time spending surfing porn and doing **** I like...

Hope this makes you less confused?
 

guest2013-1

guest
Joined
Aug 22, 2003
Messages
19,800
Oh, and if you guys have any tips on how to do the following, it would be awesome:

I'm now on "Step 2" where they can choose which database column fits in with the columns they've chosen from "Step 1". Now each of the labels/comboboxes are generated at runtime and the comboboxes obviously have the same datasource (because it maps to the same table). I'm binding each to a New BindingContext so to avoid the issue where, when you change one combobox selection, it changes all of them (because they using the same data source)

What I'm struggling with now is to dummy-proof it. I'm ALMOST to the point of getting it correct, but essentially, what needs to happen is that when they choose a specific column in the combobox, they shouldn't be able to select the same column in a different combobox.

Can't really wrap my head around that one. They're using the same data source so it's easy removing one option as soon as it's selected, but then I wipe the selected option as well, so I went ahead and "cached" the data source by cloning it for that specific selection. When I then select the other remaining columns, they too get removed/cached but the problem now exists in "resetting" them when I set one of the columns back to "Ignore" or "Select a column"... to then add the missing option to all the other options again and reset that specific combobox to use the same data source as the others...

But anyway,it's late and I don't want to confuse you guys any longer :p I'll figure it out don't worry.

And thanks again for the input!
 

Raithlin

Executive Member
Joined
Jan 4, 2005
Messages
5,049
I'm busy with a small Importer I'm writing and would like the first row of a databound datagrid in a WinForms application written in .NET to be only checkboxes.

The idea behind this is, so that people can check the columns they want to import or contains the data they'd like to push through to the database.

If I can get this working, I'm also going to add a combobox at the very top with a list of columns in the database to which the data can correspond to.

For example, I have a CSV file I successfully selected and now displaying a preview of the data in a datagrid on a form. I'd like to be able to select which columns I want to import by checkbox or by combo box situated in the first row (all columns is same object, and object is specific to that column)

Once the selection is made, and the import button hit, I'll be able to only process the columns specified and push those through to the database.

I guess I could do with 1 row of only comboboxes as well and have an "ignore" option...

Any ideas would be helpful... currently it's piss adding a column of only select boxes, but I'd like to apply that to the entire row and have that row responsible for the columns being imported or not

I'm assuming you're importing into a DataSet/DataTable, which you're then binding to the DataGrid/DataGridView. If that's the case, then adding a column to the DataTable - lets call it SelectedForImport (type Boolean) - and binding that column to a CheckBoxColumn (I'm too lazy to check if it's available for the DataGrid, but it is for the DataGridView (DataGridViewCheckBoxColumn), should do it.

For your second trick, catch the CheckboxColumn's onchange event (I would consider CellEndEdit, or using CellClick/CellDoubleClick to manually affect the change to the checkbox) - then disable the relevant column for that row (e.RowIndex).

Does that help any?
 
Last edited:

guest2013-1

guest
Joined
Aug 22, 2003
Messages
19,800
I'm assuming you're importing into a DataSet/DataTable, which you're then binding to the DataGrid/DataGridView. If that's the case, then adding a column to the DataTable - lets call it SelectedForImport (type Boolean) - and binding that column to a CheckBoxColumn (I'm too lazy to check if it's available for the DataGrid, but it is for the DataGridView (DataGridViewCheckBoxColumn), should do it.

For your second trick, catch the CheckboxColumn's onchange event (I would consider CellEndEdit, or using CellClick/CellDoubleClick to manually affect the change to the checkbox) - then disable the relevant column for that row (e.RowIndex).

Does that help any?

Adding 1 column filled with checkboxes are piss easy. One liner. Can like to do that in my sleep. But as I don't want a column filled with checkboxes, and only 1 row (the first row)... it seems that no programmer out there understands this concept...
 

Drake2007

Expert Member
Joined
Oct 23, 2008
Messages
4,413
Ah, I see the problem:

1,22,Penis
2,22,Vagina
3,22,Sex
3,23,Tricycle

We want the following:

Adult Entertainment,Penis
Adult Entertainment,Vagina
Adult Entertainment,Sex
Kids Entertainment,Tricycle

Dude/ette! your Data is corrupted!

Sorry, I'm no help either :)

but I did google for you http://www.filehelpers.com/ < maybe some code in their library to spark something.
 
Last edited:
Top