How to access ObjectDataSource object after bind to GridView - c#

I have a GridView, I bind it to ObjectDataSource. ObjectDataSource takes data from a database's table. Each row in table has unique ID.
I have a button in each row of GridView that should remove this row from database. My ObjectDataSource returns Object, this returned object contains ID (and some other information, like name, user etc.) however I do not show this ID on my grid.
Question how can I get these ID after user chooses to delete row, I need to know what I should remove.

You should assign DataKeyName property of the grid view, once you do that, you can get value of your id that you have provided in .DataKey property, which is explain in detail here with source code

If you using BindingSource, than you always can get current object.
For example somewhere in mouse click handle:
var myData = (MyData)bindingSource.Current;
MyDataRepository.DeleteMyDataById(myData.Id);

Related

How to fetch and display multiple rows data into a gridview dynamically using asp.net C#?

I've inserted multiple rows data from gridview to database at a time using stored procedure.
Now I've to fetch and display that multiple rows data into GridView and inside GridView I need to call particular value from column to a particular label into GridView.
How can I get that values dynamically? I am not seeing any way to show data.
Create a class structure. This class is passed in and defining variables for the value. Define a list of this class type. Add the individual values ​​from the database to this list. Assign this list variable to the GridView's Datasource
There are tons of articles out there just go and have a try.
Databinding
Secondly, you mentioned
I need to call particular value from column to a particular label into
GridView.
I mean on when? after binding or there should a button and it's click event do that stuff?

GridView - Hide Column but Use Value

I'm working with a GridView that I'm populating programmatically. In order to access the ID value, I have to have it as one of the columns (as opposed to a ListBox, where I was able to assign the value to each entry in a hidden way). Is there a way to hide that ID column, but still use the value?
The columns are being auto-generated through the code and I'm accessing the ID value using DataKeys.
Here's a general overview of what you can do. It's not complicated. You databind as usual, but while doing that you find the index of the ID column. Then you use that index to hide the column on the RowCreated event. If you search for how to hide a column using auto-generated columns, you'll run into several answers following the method I'm using for hiding.
Private IDColumnIndex As Integer //Class scope - we need to use it in multiple methods
Public Sub PageLoad() Handles Me.Load //or wherever your databind is happening
Dim source As New DataTable()
IDColumnIndex = source.Columns("id").Ordinal //Gets column index of "ID" column
view.DataSource = source
view.DataBind()
End Sub
//Hide our "ID" auto-generated column.
Public Sub view_RowCreated(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles view.RowCreated
e.Row.Cells(IDColumnIndex).Visible = False
End Sub
Assign the ID to the DataKeyNames collection will work fine, is that an issue for you? The data keys is available for each row, not showing up in the UI, but still accessible with the row data, so you can get the ID for each row. You just have to get it from the DataKeys collection, instead of the row directly.
You can add code to hide the first column (the ID) so that it doesn't appear, and rely on using the datakeynames to get the ID value... if that doesn't work, could you elaborate on why so I can adjust my answer..
You can get the ID value if you add the ID column to the DataKeyNames property of your GridView. msdn link here
myGrid.DataKeys[myRow.RowIndex].Value.ToString();
I usually add a visible column But then make the template for the colmn jus a space or something. Then in the ID for the column, I put the ID I want to track. This technique let's me easily find the ID of a row using javascript when the content has been rendered into HTML.
If you let the grid view do its business rendering with the hidden data keys, you may never find the ID in client side browser javascript in all that mess.

How to get value of a particular cell in gridview i and pass it to db as a parameter in c#?

i m using C#, i am having problem in retrieving value of particular cell in gridview. the scenario is, there is a list of jobs shown in my grid. Coloumns are job_id, and job_title. I am populating my grid from database, so the grid shows a list of jobs available. and client can click on job_title, which is a hyperlink (
you can use DataKeys Property
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.datakeys.aspx
You can embedd job_id in hyperlink - hyperlink can be part of you database (one extra column) or created when you are populating your grid with data from database.

SelectedIndexChanged fires a detailview grid but how to catch it's info

This is how i have it setup:
DAL: a dbml file for the context
BLL: All my BL rules
Types: I made type for every object that i return to my presentation layer.
Now that i made types and use this structure i find it hard to understand how to manipulate data. Normally i could just bind my datacontext to a grid and just activate all the datagrid's possibilities without having to worry about anything.
Now for example when i want to enable sorting, i have to write my own logic to return teh right rows to the grid etc.
I have a multiview with two datagrids. The first datagrid is an overview grid and has an option to select an entry. When i select the entry i switch to my second view and I want to show the details for the selected object there.
However... I bound a List to the first grid and now I don't know how to find out what entry is selected. I have an event handler for the SelectedIndexChanged.
I want to catch the right details for the 2nd datagrid there.
assuming that your objects from the BLL have an Id property, apply a the name of the property as a datakey to the first GridView. In the aspx page, add the following property to the gridview
DataKeyNames="Id"
On the SelectedIndexChanged, get the Id of your selected object using the following code:
string id = myGrid.SelectedDataKey.Value;
Now that you have your Id, you can query your needed object from the DB.

Accessing a property in a Datagrid in C#

I am loading the output of a database query to a DataGrid.
myAdapter.Fill(ds,"AllInfo");
dataGridSearchOutput.DataSource = ds.Tables["AllInfo"].DefaultView;
It will fillup the data grid control with multiple records. Suppose the primary key of the data record is "ID".
Now I want to generate report. To do that I need to select an item and click a button "Generate Report" or double click a record. Then a report should be generated for that ID.
My question is How should I capture the ID of the record? In otherwords I need to read the selected value in datagrid.
You could try using
dataGridView1.CurrentRow.Cells["ID"].Value
Have a look at
DataGridView.CurrentRow Property
get the selected row in datagridview
EDIT:
Maybe then have a look at using DataGrid.CurrentRowIndex Property
Will this work? Put it in the event handler for the click or double click...
DataView dv = (DataView)dataGridSearchOutput.DataSource;
DataRow selectedRow = dv.ToTable().Rows[dataGridSearchOutput.CurrentRowIndex];
long id = (long)selectedRow["ID"];
I think the most easy and bets approach is set GridView property DataKeyNames to ID and then you can have the ID easily using
int index = dataGridSearchOutput.SelectedIndex;
dataGridSearchOutput.DataKeys[index].Value.ToString()
or you can convert it long as it is object.

Categories