1
Hey
Cosider the following points
GridView is showing an extra blank row
> This is because, you have to set the property AllowUserToAddRows to false, by default it is true, this will remove the extra blank row.
it shows the same item twice
> I prefer to use KeyUp or PreviewKeyUp event instead of ..KeyDown event.
For deleting purpose, why just don't you let it done this automatically, when user press delete key, it will be removed automatically, why using double click for deletion.
Good Luck
Accepted 1
Hey
Do the following process to achieve your task in a much easy way
1. Take a global Datatable as
- DataTable dt =new DataTable ();
2. Bind this DataTable with your gridview
-
- dt.Columns.Add("Items", typeof(string));
-
- gridView.DataSource = dt;
3. When adding combobox items to gridview, add items to dt
-
-
- var row =dt.NewRow();
- row["Items"]= combox.selectedItems();
- dt.Rows.Add(row);
4. Let the items of the gridview to be deleted automatically (i.e by delete key)
5. When you want to save your gridview data then process dt.Rows, this will give your data.
6. That's all you have to do.
Good Luck
1
Q
When you have set the dataSource as its items then you cannot add/delete items with
Xx.items.add(..)
Xx.items.RemoveAt(..)
0
Hiii,
i was a bit confused about how to used the codes that you show because both combobox values come from database and the second combo is depenent on the first combo.
but finally i used...
DataTable dt = dgvItems.DataSource as DataTable;
dt.Rows.Add(cmbItems.SelectedText);
in Items KeyUp event and it's done same as i wanted..
Sorry for late reply...
and thank you very much for your help and patience.
Mayank Jani.
0
Hiii
sorry pal, i can not understand what you are trying to say, "As you had used dataSource (datatable) then add your combobox items to that datatable, it will get reflected to your gridview."
there are two datasources (datatables), both bounded to combobox. 'Group' is main and 'Items' is sub. which dt to add combobox? i only want 'Items' combo to be added in gridview.
can it be done with copy and paste? i have searched on net and found something to use of array, i am trying to use array but still not made it. i am trying to copy gridview in an empty array then at the time of edit the gridview, first copy the grid to an arrya and then clear the gridview and paste in as a new gridview.
can you give some idea to rectify the process? if you found it ok.
Thanks
Mayank Jani
0
As you had used dataSource (datatable) then add your combobox items to that datatable, it will get reflected to your gridview.
0
Hiiiii
Xx.items.RemoveAt(..) works ok after load the datagridview but the error occures at the time of adding a new row.
Could you please let me know any other way to do this?
Thanks
Mayank Jani.
0
Hiii Amit,
that's not working.
please see the attached files. i have attached both the screen shots and the code. Please note that i have write necessary comments through out the code. i hope that will help you to understand the code and the purpose too.
let me try to explain... all other functions are working fine except, the update. i want to update the grid view by removing the incorrect and adding the correct, then save. after remove (or even without remove) the incorrect row, when i try to select the correct entry form the Items ComboBox, i got the error.
or in other words, i can not add any items when the application is in Update mode.
please see the both errors in screen shots.
thank you for your patience.
Mayank Jani
0
Unfortunately I couldn't able to understand your question. Why using ColumnCount =1 when your code seems to add the selected combobox items to gridview
I believe that trying the below will work as expected
- private void cmbItems_KeyUp(object sender, KeyEventArgs e)
- {
- if (e.KeyCode == Keys.Enter)
- {
-
-
-
- dgvItems.Rows.Add(cmbItems.Text);
-
-
- }
0
Hii Amit,
Would you please take a look at this error? after your suggestion it workd. now it's edition, at the time of edit the record, when i delete the incorrect entry and try to insert the correct item into the grid view, it shows the error that 'ColumnCount property cannot be set on a data-bound DataGridView control.'
please see the attached code files.
1.select any group from 'Select Group...' combo box.
2.it will load the group name in 'New Group' text box.
3.also it will load items for the group in the grid view.
4. remove any item by double click on any cell.
5.try to add new item from 'Items' combo box by select and press enter.
6.the error....
please help.
thank you
mayank jani.
0
Dear Amit,
Thank Youuuuu sooooooo much... :)
it's done. just the third matter remains, I have keep the AllowUserToDeleteRow to True but still I can't delete the selected row by hit the enter key. Is there any other setting remains to do? (Please don't think that why this guy don't understand this kind of easy things!! ;)) menawhile, the double click to remove a row is working fine now. :))
and would you please let me know about any website or something on net which can help me to understand the grid view (C# WinForm) much better and thoroughly. I mean not all but, mostly used by developers?
Thanks a lot once again...
Mayank Jani