In ASP.NET, Can I Delete a row in GridView while the primary key of that table is not loaded into the gridview? - asp.net

I have a table with primary key whose importance is technically high and none business wise.
So, when I display the table data to user, I would like not to show him the primary key.
Even if I load it in sqldatasource and dont show (I did this by removing the cloumn in "Columns" tag of GridView), I am not able to update or delete row using the built-in UpdateCommand & DeleteCommand.
I have created the delete paramters but it does nothing when I hit the delete button.
How to get around it?
Thanks
R S Reddy

Use the primary key as the CommandArgument for the delete button. This say, you still have server side access to the value you need without having to expose it on the front end.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.commandargument.aspx

Well, I got this fixed by using DataKeyNames attribute in GridView.
Just added primarykey of my table into it and everythings working like charm.
Thanks for your prompt help Babak.

Related

GridView not updating when data comes from multiple tables

I have a GridView control that gets data from two tables, the first one contains a primary key, a name (string) and a foreign key to the second table, The second table contains a primary key "referenced by the foreign key mentioned" and a name (string), i was able to display the id, name (first table) and the name (second table) using an inner join but i can't update the data in the tables using the GridView (when pressing update nothing happens at all, or no change occurs).
From what i understand of your question what you have is a situation that you must a apply a nested gridview.
A gridview shows the content of a table (datatable or a collection).
If you want to show other collection that is inside each row of your primary gridview, you will need
to build a second Gridview or listview or repeater to show that information.
with the primary griview you can use the OnItemDataBound to assign the datasource of the nested gridview or what you choose to show that information based in primary key of the row.
Don't bother i found a solution to the problem on Microsoft website, i am sorry i didn't make my question pretty clear and here is a link to what i was looking for: Editing with Template Fields
Maybe after clicking Update you need to call your read method again to refresh your gridview.
something Like:
UpdateMethod()
{
//YOUR UPDATE STUFF
//REBIND DATA WITH UPDATED RECORDS
RefreshMethod(); //YOUR BINDING METHOD TO DATAGRID STUFF
}

how to add data into a specific column at runtime in grid

I have a rad grid, i have bounded the columns in the grid using GripBoundColumns which shows me dropdown cloumns when i edit the record, Insert/update/delete are working fine for me.
My question is it possible to insert new data into the column(Not the whole record just only to one column) when i edit the record.
For example...
suppose i have 5 columns (Client Name, Account No, Account name, account status, Custodian Dealer)
CustodianDealer is my drop down coloumn and data for it comes from different table
when i edit or insert a new record in the grid, i can select the existing Custodiandealers in the table.
now if i want to insert a new record to the custodianDealer table not to the grid, how can i acheive it
Try something like this within the ItemDataBound event:
If TypeOf (e.Item) Is GridDataItem Then
Dim GDI As GridDataItem = CType(e.Item, GridDataItem)
GDI([ColumnNameAsString]).Text = "Some custom text"
End If
The GridDataItem is essentially a TableCell, so you can add controls as needed.
Oh, and if it wasn't clear from my notation, "[ColumnNameAsString]" is your own column name.
Wire the grid insert command and execute an insert query which updates merely the Custodiandealers table with a new entry. To display it in the dropdown editor, make sure the new record has a foreign relation with the grid main table source as the rest of the items in the Custodiandealers table.
So, if I'm reading your problem correctly, you are trying to insert a record into the custodianDealer table based on some changes made during edit/insert.
If that's the case, you can handle the ItemCommand event and update your datatable based on those changes.
Can you provide the exact details of the use case?

Easiest method to build where clause from checked items in DataList of PreviousPage

I have a selection list that is generated dynamically, it lists a number of checkboxes that are based on an end-user editable table, as such I have no idea what data, or how much, might be contained in this table and how many checkboxes or what they might contain, other than the primary keys of the table.
The user will select the checks they wish to see, and then control is passed to another page via PostBackUrl. The second page has to figure out which records to show (build it's where clause) based on the checkboxes checked in the previous page.
So, my problem is several-fold. First, asp:CheckBoxes don't have values. This can be worked around by a number of methods. Right now, i'm using a placeholder and dynamically creating the checkboxes in the ItemDataBound event of the DataList. I set the ID to "CheckboxKey1Key2" (where Key1 and Key2 are the primary keys of the check items).
Second, I have to walk through the controls of the PreviousPage to dig out all these values. That in itself is also a pain, but doable.
Now, my thinking is to build the where clause of my Linq2Sql query based on the keys I got from decoding the checked checkbox names. This all seems like a lot of jumping through hoops for something that shouldn't be this difficult. Am I missing something? Does anyone have any better solutions?
Make a class with a structure for your values that will be useful and easy for you to use on the result page. Then when the user clicks whatever it is they click to go to the result page, loop through the DataList, create a collection from the checked items, and you will only need to grab one object instead of everything, when you need to format your query.
Then when you get to the result page, loop through the collection and build the query in the loop. Pretty easy and not much code.

Deleting GridView’s rows

Let’s assume that ( the first time page is created ) I manually bind GridView to some data source. Is there’s a way to configure GridView to delete a row ( by pressing row’s delete button) without handling any of the delete events ( in other words, we wouldn’t try to delete a row in data source and then rebind GridView to it)? Instead, GridView would simply remove that row from its ViewState and then display all rows minus the deleted one
thanx
So then how would you end up remembering this and deleting it in the db?
Why not do what may be simplier is to have a bit field in your table called
Deleted. That way when someone deletes something accidentally it is still there.
Your record set would make use of a WHERE clause to filter Deleted=False
Instead of binding the GridView to your data source, bind it to a collection built from your data source. You can then add to, delete from and modify the collection as much as you want, without ever affecting the underlying database table.

Store Timestamp in GridView

I have a table with three columns that I need to display on a ASP.NET page. (SQL Server 2005, ASP.NET 2.0)
id int
value varchar(50)
tstamp timestamp
I use the timestamp field to handle concurrency validation so it's for internal use only and will never be displayed to the end user. But I need to store it somewhere in order to do proper updates.
Here's my update sproc.
UPDATE ValueTable SET value = #value
WHERE (id = #id) AND (tstamp = #tstamp)
SELECT #tstamp=tstamp FROM ValueTable
WHERE id=#id
I use a SqlDataSource to connect to my database and the schema has all three columns. My grid view will only display two fields since the timestamp field is hidden (Visible=False)
When I profile my asp page it looks like it doesn't store the timestamp anywhere even though I have a "hidden" field in the table.
How would you store a timestamp value in general on a web page? It should never be displayed, but it is needed for any updates.
After a lot of playing around I think I found a half-decent solution.
The problem seems to be that fields that are "Visible=false" are not included or bound in any update or delete commands.
The HiddenField works fine for Update commands, so if you are not doing any Delete commands you should be fine by including a hidden field which is bound (Bind(TStamp)) to the timestamp column in a templated field.
The problem is that if you are doing Deletes as well it doesn't look at any bound, hidden fields.
What I came up with was to add the timestamp to the data keys of the grid view. That way it will be considered a composite key together with the ID.
So, in short, add the timestamp to the DataKeyNames of the GridView/DetailsView etc, and remove any visible fields. That seemed to do the trick.

Resources