I have a GridView that gets its data from a SQL Server stored procedure. There are Item Templates in the grid to allow for data entry. I have a column with a CommandButton at the end of each row.
What I'm trying to accomplish:
If the user clicks the button, a new row gets inserted which is a duplicate of the previous row. Scenario: while doing data entry, an item has more than one bin location. By duplicating the row, the end user can enter the 2nd bin location, where both records get dumped to a database.
I've been looking around and trying to filter out the remove duplicates from my research. Can this be done without putting the GridView into a datatable and rebinding? Is there a simpler way to do it server side? Does it need to be added in the footer?
Unfortunately, you can't use the same row or same cells because the DataGridView only takes unique rows and cells. You'll have to get the values of that row and use them in a new row. I wrote a little bit of code to do this
string[] cells = (from c in datagridview.Rows[e.RowIndex].Cells.Cast<DataGridViewCell>()
select c.Value.ToString()).ToArray();
DataGridViewRow row = new DataGridViewRow();
row.CreateCells(datagridview, cells);
datagridview.Rows.Add(row);
I hope I understood your question right. This is just something to get you started. I'm pretty sure it will not resolve your problem completely, but it's something.
Related
I have a DataGridView filled from a DataSet with one blank row at the end (because of CanUserAddRows = true by default). When I fill-in some data in that row and close the DataGridView, I check HasChanges which turns out to be false and it prevents me from updating the DataSet correctly (changing data in the existing records works as expected). Debugging it showed that that DataSet has one table with only the pre-existing (non-blank) rows, so that probably is the reason it does not detect any changes (since that blank row is/was not part of the DataSet). How to solve this problem? Maybe I have to detect somehow that the focus is in a cell from that blank row and add a new record to the DataSet. Is there an automatic way to deal with it? (It looks like a pretty common situation.)
In c# how to add new row in to a datagridview after the existing row.
I tried grdshedule.Rows.Add();
But all the rows are adding before the existing row
Make sure that AllowUserToAddRows property of datagridview is False. And like u tried before programmaticly ;
datagridview.Rows.Add();
grdshedule.Rows.Insert(grdshedule.Rows.Count-1, newRow);
Your rows are not getting automatically sorted hence you cannot see the new row at the bottom, this means once you will have to resort your rows after adding new row
To sort please example http://msdn.microsoft.com/en-us/library/0868ft3z.aspx
**Line Taken from MSDN Remarks**
Rows in the control are not automatically sorted when new rows are added.
Please read full remarks from MSDN http://msdn.microsoft.com/en-us/library/x0akdhcd.aspx
I had the same issue with DataGridView when it came to adding a new row after existing rows or at the end of the table.
After spending some time on this, I figured out why I couldn't add rows at the end of the table. It's not that the datagridview had any issues or restrictions, but it was really in my code. However, datagridview does add new row at the end of the table every time.
I really wish you posted some part of your code it will help a lot in giving you an answer.
Anyways, in my program I have a class from which I create list of objects. My List is actually an ArrayList and I display these objects in the same order they are in the list. Whenever I add or delete an object from the List, it is reflected 1-to-1 on the datagridview. In other word, if the objects were listed in reverse order in the ArrayList, then that's the order the objects were shown in the Datagridview. That doesn't mean the rows are being added on top of your existing rows, although it looked like it for me.
My guess is that you have a list of some sort that you are maintaining in your program. This list is where you get your information to display on your datagridview. If I am correct in saying that, then you probably are inserting your items in your list somewhere in your program based on the row that is currently selected on your datagridview. If that is the case, then your current item is being inserted into the position of your selected items in your ArrayList. This causes your list to shift everything by one from the selected item in your ArrayList. Thus, when you display your list of items in your datagridview, it looks like the new row is not being added at the end of the table.
To resolve this issue, first you need to add a new empty row:
DGrid.Rows.Add;
Then, insert your new item at the very end of your list:
var newitem := new Object;
ArrayList.insert(DGrid.RowCount-1,newitem);
Then, update your datagridview based on this ArrayList. Now, your datagridview will act as you expected.
Before my fix, my datagridview kept acting like it was inserting new rows on the top and in reverse order.
Is there are a way to 'validate' the contents of my data grid in windows form whether it is a duplicate copy or not?
I have a combo box inside my data grid and what I want to do is every time I add a new row, the user selects an item from the combo box. The next time the user adds a new row with the same entry from the previous ones (duplicate), it will not be added to the row.
I was thinking of removing or disabling the item from the combo box (datasource from the database) upon adding to the new row, so the user will not be able to duplicate the record.
What would be the best approach on this problem? Comments and suggestions are welcome. Thank you.
You are planning to do the right thing. Removing the item from the combo is not a bad idea. Or else you can do a validation at the time of adding a new row to check if the item is already there in the grid.
Since the data source for your combo box is from a database why don't you just modify the query not to include items that are in the rows of your table, something like
SELECT item FROM itemList WHERE item not in (SELECT item FROM userAddedRow)
Instead of Going to Database u can perform your check operation in your Dataset.
Perform Check operation From Dataset and if not Exist then send the Insert statement
to the DataBase.
Hopefully simple, but can't find any such option.
I have a data table -- has say... 10 rows in it. Some fields on the form are bound to the table.columns respectively by name.
On another form that HAS a grid, as I scroll the grid, the detail fields are refreshed as expected since the grid does some magic to trigger the DataTable record changing event.
WITHOUT using a Data Grid, How can I direct the table to go to a specific row for load/display refresh on the form... ex:
DataTable MyTable = new DataTable();
MyTable = GetResultsFromSQL(); // returns the 10 rows
MyTable.LoadTheDataForRow(3);
MyTable.LoadTheDataForRow(7);
MyTable.LoadTheDataForRow(2);
I know I can use a foreach row in the table, but need explicit use as I don't want to go through all rows, but need specificity to specific ones.
I've looked at the LoadDataRow(), but that appears to be for pushing data back to a server. NOT what I want... I just want to have the "Current" row of the table to be of a specific one...
Thanks
After further research, I've found that a FORM based control "BindingSource" (or derivative) allows this, such as a grid. But, obviously, there's something the .Net engine is doing under the hood to ultimately "Load" a given row into something that ultimately triggers back to the "BindingSource"... The DataTable has RowChanging and RowChanged events which appear to be triggered by OnRowChanging / OnRowChanged delegates, but how can we tell the data table which "row" we want it as the active one.
The form controls can do this for their binding sources, but what is really happening under the hood to trigger these OnRowChanging events... I don't want to re-load a data table, rows, etc, just change what is considered the "Active" row, as in a grid, listbox, combobox, etc.
Have you tried:
MyDataTable.Rows[3];
MyDataTable.Rows[7];
MyDataTable.Rows[2];
Before anyone suggests scrapping the table tags altogether, I'm just modifying this part of a very large system, so it really wouldn't be wise for me to revise the table structure (the app is filled with similar tables).
This is a webapp in C# .NET - data comes in from a webservice and is displayed onscreen in a table. The table's rows are generated with asp:Repeaters, so that the rows alternate colers nicely. The table previously held one item of data per row. Now, essentially, the table has sub-headers... The first row is the date, the second row shows a line of data, and all the next rows are data rows until data of a new date comes in, in which case there will be another sub-header row.
At first I thought I could cheat a little and do this pretty easily to keep the current repeater structure- I just need to feed some cells the empty string so that no data appears in them. Now, however, we're considering one of those +/- collapsers next to each date, so that they can collapse all the data. My mind immediately went to hiding rows when a button is pressed... but I don't know how to hide rows from the code behind unless the row has a unique id, and I'm not sure if you can do that with repeaters.
I hope I've expressed the problem well. I'm sure I'll find a way TBH but I just saw this site on slashdot and thought I'd give it a whirl :)
When you build the row in the databinding event, you can add in a unique identifier using say the id of the data field or something else that you use to make it unique.
Then you could use a client side method to expand collapse if you want to fill it with data in the beginning, toggling the style.display setting in Javascript for the table row element.
just wrap the contents of the item template in an asp:Panel, then you have you have a unique id. Then throw in some jquery for some spice ;)
edit: just noticed that you are using a table. put the id on the row. then toggle it.