473,396 Members | 1,916 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 12528
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: theKirk | last post by:
using Visual Studio 2005 C# ASP.NET I know there has to be a simple way to do this....I want to use C# in a code behind for aspx. Populate a GridView from an xml file Add Fields to the...
3
by: Mark Parter | last post by:
I have a GridView control which is bound to a Stored Procedure (SP) using a SQLDataSource. The problem I'm having is that the SP returns 4 known columns and x amount of unknown columns. I can...
1
by: | last post by:
I'm wondering if it's possible, outside of the declaration of an ASP.NET 2.0 GridView, to programmatically style individual columns. For example, I might want to make the text in a particular...
7
by: | last post by:
Hello, Does anyone have an idea on how I can filter the data in the gridview control that was returned by an sql query? I have a gridview that works fine when I populate it with data. Now I...
5
by: Brian McClellan | last post by:
Just wondering if anyone has a simple example of creating a gridview completely programmatically, i'm not doing anything terribly sophisticated. When creating the gridview declaratively evertying...
3
by: misiek | last post by:
Hi all. I have following problem: 1. In my web page I have a GridView control, which does not have a DataSourceId set in designer. 2. When user presses Start button then I create...
4
by: Mike | last post by:
I'm having trouble getting a gridview to bind. I probably missing something completely obvious and would appreciate any help on offer. I'm passing parameters via querystring, and have created a...
2
by: antonyliu2002 | last post by:
I've been googling for some time, and could not find the solution to this problem. I am testing the paging feature of gridview. I have a very simple web form on which the user can select a few...
3
by: Polaris | last post by:
Hi Experts: I'm using asp.net 2.0 with Visual Studio 2005. I'm trying to use the GridView to display data. I need to programmatically add rows into the GridView. So far could not find a way...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.