Get ObjectDataSource records in code-behind - c#

I have a ObjectDataSource which is placed in de source code of my .aspx page, not the code behind, and is used inside an EditTemplate column of a datagrid.
<asp:DropDownList ID="ddlist1" runat="server" DataSourceID="osCreditType" ...
After selecting a value in the dropdown and setting the datagrid state back to ItemTemplate, I have the ValueMember of that DropDownItem in the NewValues collection inside the RowUpdating Event.
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int primary = int.Parse(Convert.ToString(e.NewValues[0]));
}
Now, I want to retrieve some other information from the ObjectDataSource using that primary.
Is the data, used for the dropdown still available in the ObjectDataSource, or will a call to that source from code-behind make the datasource go back to the database
Can I use the ObjectDataSource to retrieve additional information using this primary key, and if so, how do I accomplish it ?
Thanks a lot in advance

You have to remember that the ObjectDataSource is really
just a Binder between your Data Layer and Your Controls.
What would be better is for you in the GridView1_RowUpdating routine
is to use a SqlDataAdapter and go and get the information from the database yourself
and then use that information to change the values of the Updated Row.
You will have a problem trying to run another Query with the Same ObjectDataSource,
cause as soon as you do that the control will try to rebind to the new data.
Hope this helps.

Related

Gridview Get Original Values in OnRowUpdating method

I am not setting my Gridview DataSource to a control like SqlDataSource or ObjectDataSource.
Instead I am Binding manually in the code behind using a method I wrote:
protected void Bind()
{
CustomDepartmentGV.DataSource = GetSortedDepartments();
CustomDepartmentGV.DataBind();
}
I am also manually handing update through the Gridview properties:
OnRowUpdating="UpdateRow"
And a simplified version of the UpdateRow method in the code behind:
protected void UpdateRow(object sender, GridViewUpdateEventArgs e)
{
var oldValues = e.OldValues;
}
When I run the code above the oldValues variable is set to a System.Collections.Specialized.OrderedDictionary class but the keys and values collections within that have 0 keys / values.
After researching this quite a bit I found a couple posts that say that e.OldValues is not set unless you set the Gridview DataSource to a control like SqlDataSource or ObjectDataSource. I do not want to use these as I like the flexibility it gives me to write my own methods in code behind before hitting the data access layer objects.
Does anyone know how I can get e.OldValues working or provide an alternative so I can look at the original values of the row being updated in the UpdateRow method?
Note
I am able to get the original values of the Gridview DataKey fields but I obviously don't want to set every column to be a data key.
Suppose the GetSortedDepartments() method returns a Datatable. You can put this datatable in the session (for example) and in the UpdateRow method use the Datakey of the the current row to get the old values from this datatable.
Don't forget to update the datatable and to put it again in the session after a row is updated.

Data in Telerik RadGrid not updated using LINQ-to-SQL (strange behavior)

I've found a strange behavior with Telerik RadGrid Datasource using Microsoft LINQ To Sql, which I'd like to know what reason causes it.
Suppose I've a basic grid with OnNeedDataSource event:
<telerik:RadGrid ID="grid" runat="server" OnNeedDataSource="grid_NeedDataSource">
...
</telerik:RadGrid>
Event handler in code-behind:
protected void grid_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
grid.DataSource = Dal.Db.VIEW_DETAILs;
}
On the Microsoft Sql Server, VIEW_DETAIL is defined as a view (which select query joins multiple ordinary tables). One of the source tables (let's say it's name is DETAIL) for the view query uses also a computed column that's present also in the view. Querying the view directly from Sql always returns consistent and updated results.
When in my program the source table DETAIL is updated, I call normally
grid.DataBind()
to update its content. Surprisingly nothing is updated (for instance the computed column mentioned above remains the old value). After dealing with some workarounds, I've found that changing the source above on the grid_NeedDataSource handler to
protected void grid_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
grid.DataSource = Dal.Db.VIEW_DETAILs.Where(x=> 1 == 1);
}
(e.g. adding a Where clause that is always true) the data now is correctly updated.
Any insights on this behavior?
Try calling Rebind() instead of DataBind().
According to Telerik, when you need to reassign the datasource of a RadGrid, you should be calling Rebind(). Check the 2nd section Misusing or Not Using NeedDataSource Event on this Telerik page here for more.

AspxGridView initially load empty

I asked this question here, too: http://www.devexpress.com/Support/Center/p/Q388764.aspx
but I hope for a quicker answer here.
I need my grid to display data only after the user starts filtering.
This is the scenario:
When opening a page containing the grid, the grid should no even try to display the data.
When the user starts searching (applies a filter), the data should be displayed.
Is this possible?
Thanks.
Ref: this
Subscribing to DataBinding event resolved all troubles with correct behavior actions like sorting, filtering and groupping events of ASPxGridView in runtime mode with requirement of getting DataTable for ASPxGridView.DataSource.
And you have answered at devexpress also it was also using the idea to implement this as i have looked by above reference link:
protected void gvData_DataBinding(object sender, EventArgs e) {
if (Convert.ToBoolean(Session["need_bind"]))
gvData.DataSource = DSource;
else
gvData.DataSource = null;
}
along this use ASPxGridView.ProcessColumnAutoFilter event handler
Do not bind the data in the Page_load.
Bind the grid to the datasource in the filter event/ search button click event..
Make the grid default to a filter that returns no records. The end user can then filter\search themselves and the ajax callbacks will repopulate the grid.

C#.Net : Edit and Update the dynamically bound grid view

In my website I am dynamically binding the datasource to the grid based on the value in dropdownlist.
I want to edit and update the values in grid view and the respective updations should be done in the original database.
How can I do that?
In the most basic and direct way, you use the OnUpdateCommand event of the datagrid to invoke a server site handler. That handler will receive a DataGridCommandEventArgs parameter containing an Item property which is a grid row with updated values. Retrieve key and new values from that row and build a corresponding update command.
do you know how to bind dropdownlist vai datasourase?
and witch data sourse you use tell me first.
else you just make a SELECT query and fill the dataset after that you you must have to bind dropdownlist like this....
ds = dropdownlist.DataBind();
i think this help you.......
else you can tell me if any problem occure in this code..........

Setting GridView's DataSourceId to "", causes GridView to lose

If I bind GridView to SqlDataSource, then the first time page will be created, GridView will display columns retrieved from data source. But if on postback I set GridView.DataSourceID to null or to an empty string
protected void Page_Load(object sender, EventArgs e)
{
...
if (IsPostBack)
GridView1.DataSourceID = "";
...
}
, then GridView won’t display any rows at all. But why is that?
Assuming GridView has EnableViewstate set to true, then it should be able to display rows it retrieved from data source when page was first created!
I realize one could argue that Framework notices that DataSourceId has changed and assumes GridView doesn't need data from previous data source, but I’d assume Framework would realize that empty string or null reference doesn’t point to any of data source and thus wouldn’t remove any data GridView retrieved from previous data source?!
thank you
EDIT:
Hello,
Basically what is going on is once you have set the DataSourceID to null or an empty string the control takes this as an indication from the consumer of the control that they do not wish to bind any data at all (even ViewState data). The control checks the DataSourceID prior to binding and if it is an empty string then it does not DataBind in its EnsureDataBound method.
So if you set DataSourceID to null inside Page_Load(), but GridView only checks DataSourceId moments before binding ( which happens much after Page_Load ), then until DataSourceId is checked, ViewState containing data from previous data source should still be available inside Page_Load() ( and still available,for example,inside an event handler subscribed to an event that caused a postback)?!
Your control will not rebind to the ViewState data if you set the DataSourceID to null or an empty string. The article I linked below has an excellent explanation as to why.
Basically what is going on is once you have set the DataSourceID to null or an empty string the control takes this as an indication from the consumer of the control that they do not wish to bind any data at all (even ViewState data). The control checks the DataSourceID prior to binding and if it is an empty string then it does not DataBind in its EnsureDataBound method.
System.What?: DataSource VS. DataSourceID (Internals)
I’d assume Framework would realize
that empty string or null reference
doesn’t point to any of data source
and thus wouldn’t remove any data
GridView retrieved from previous data
source?
Why do you think that way, you reset the value, of course, the girdview has to be bind the new value you reset.

Categories

Resources