472,119 Members | 1,527 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,119 software developers and data experts.

Wiring a GridView Programmatically - Is It Possible??

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.
Mar 13 '07 #1
4 12457
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.Categories TableAdapter", "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 (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.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/subscripti...t/default.aspx.
==================================================

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

Mar 14 '07 #2
Walter,

Thank you for your very helpful response!

Sincerely,

Tomasz

"Walter Wang [MSFT]" <wa****@online.microsoft.comwrote in message
news:K5***************@TK2MSFTNGHUB02.phx.gbl...
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.Categories TableAdapter",
"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 (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.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/subscripti...t/default.aspx.
==================================================

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

Mar 14 '07 #3
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
"Walter Wang [MSFT]" <wa****@online.microsoft.comwrote in message
news:K5***************@TK2MSFTNGHUB02.phx.gbl...
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.Categories TableAdapter",
"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 (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.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/subscripti...t/default.aspx.
==================================================

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

Apr 10 '07 #4
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 (wa****@online.microsoft.com, 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.

Apr 12 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Mark Parter | last post: by
5 posts views Thread by Brian McClellan | last post: by
4 posts views Thread by Mike | last post: by
2 posts views Thread by antonyliu2002 | last post: by
3 posts views Thread by Polaris | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.