Wiring a GridView Programmatically - Is It Possible??

T

Tomasz

Hello Developers,

Here is interesting problem I just came across: how do I wire a GridView
control programmatically?

Here is my sample code using Object Data Source:

protected void Page_Load(object sender, EventArgs e)
{
ObjectDataSource ods = new ObjectDataSource("MyTestTableAdapter",
"GetData");
GridView1.DataSource = ods;
GridView1.DataBind();
}

My GridView1 has auto generated Edit and Delete buttons, and of course, they
do not work. An attempt to modify data causes "The GridView 'GridView1'
fired event RowEditing which wasn't handled" exception.

Ok, I know that it would not update anyway, since UpdateMethod and
UpdateParameters properties are not set, but it seems that it fails before
even trying.

The same control wired using a designer (DataSourceID property) behaves
differently, that is: text boxes are being rendered, "Edit" button change to
"Cancel" and so on.

It almost seems that it is not possible to achieve the same effect
programmatically *without handling all the required events*.

Please prove me if I am wrong. I hope I am wrong, and it is not a sign a new
paradigm in MS component development: use a designer or you are screwed.

Thank you,

Tomasz

**Note: I know how to bind GridView1 programatically using templates and
handling all the events like RowEditing or RowDeleting.
 
W

Walter Wang [MSFT]

Hi Tomasz,

This is really an interesting question. I understand that your objective
here is to programmatically create an ObjectDataSource instance at run-time
and want to bind your GridView to it. So far you're seeing that the data
displays but editing it will require you manually handle the OnRowEditing
event.

With Reflector (http://www.aisto.com/roeder/dotnet/), you could see that
GridView.OnRowEditing will throw the exception if the DataSourceID property
is not set.

To workaround this limit, I'm afraid you will have to associate the
ObjectDataSource with the GridView using DataSourceID property instead of
DataSource. To do that, we will also have to add the ObjectDataSource to
the same container of GridView:

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

protected void Page_Load(object sender, EventArgs e)
{
ObjectDataSource ods = new
ObjectDataSource("DataSet1TableAdapters.CategoriesTableAdapter", "GetData");
ods.OldValuesParameterFormatString = "Original_{0}";
ods.UpdateMethod = "Update";
ods.UpdateParameters.Add("CategoryName", TypeCode.String, null);
ods.UpdateParameters.Add("Original_CategoryID", TypeCode.Int32,
null);
ods.ID = "ods1";
form1.Controls.Add(ods);
GridView1.DataSourceID = ods.ID;
}
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True"
AutoGenerateColumns="False" DataKeyNames="CategoryID">
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:BoundField DataField="CategoryID"
HeaderText="CategoryID" InsertVisible="False"
ReadOnly="True" SortExpression="CategoryID" />
<asp:BoundField DataField="CategoryName"
HeaderText="CategoryName" SortExpression="CategoryName" />
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>

Hope this helps.

Sincerely,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications. If you are using Outlook Express, please make sure you clear the
check box "Tools/Options/Read: Get 300 headers at a time" to see your reply
promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
T

Tomasz

Hello Walter,

I have started using the method you suggested.
However, I discovered an interesting problem I decided to described here for
the record.
GridView and DetailsView controls bound that way behave slightly
differently.

DetailsView.DataSourceID should be specified early, in Page_PreLoad event.
Otherwise some validators attached to DetailsView template controls may not
work properly.

On the other hand, GridView.DataSourceID must be specified in Page_Load
event, *even during PostBack*. Otherwise GridView will not refresh data.
That is, it will not call ExecuteSelect() on the
DataSourceControl.DataSourceView.

Tomasz J
 
W

Walter Wang [MSFT]

Hi Tomasz,

Thank you for sharing your findings with the community.

Yes how to programmatically set DataSourceID is not documented and there
might be some difference in different controls.

Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top