Gridview samples

  • Thread starter Thread starter Alex. O. Koranteng
  • Start date Start date
A

Alex. O. Koranteng

(1)I am now working on the CustomizingEditingUI folder for the default
folder. It shows the pull down list control on edit mode, great feature I
ahve always wanted to learn how to do. However the product name field is
showing the same value. The link to this page is
http://msdn.microsoft.com/en-us/library/aa479339.aspx and the specific topic
is Editing the underlying data

Any suggestions
 
Hi Alex,

My name is Allen Chen. It's my pleasure to work with you on this issue.

First, could you clarify what do you mean by this?

Quote from Alex==================================================
However the product name field is
showing the same value.
==================================================

Could you provide your current aspx code and tell me your requirement in
detail?

Regards,
Allen Chen
Microsoft Online Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 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. 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/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Allen,

Below is the code for the displaying data page:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<script runat="server">

</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:ObjectDataSource ID="productDataSource" Runat="server"
TypeName="ProductDAL"
SelectMethod="GetProducts" EnablePaging="True"
SelectCountMethod="TotalNumberOfProducts" UpdateMethod="UpdateProduct">
<UpdateParameters>
<asp:Parameter Name="ProductID" Type="Int32" />
<asp:Parameter Name="productName" Type="String" />
<asp:Parameter Name="unitPrice" Type="Decimal" />
<asp:Parameter Name="unitsInStock" Type="Int32" />
</UpdateParameters>
</asp:ObjectDataSource>
<asp:GridView ID="GridView1" Runat="server"
DataSourceID="productDataSource"
AutoGenerateColumns="False" AllowPaging="True" BorderWidth="1px"
BackColor="White"
CellPadding="4" BorderStyle="None" BorderColor="#3366CC">
<FooterStyle ForeColor="#003399"
BackColor="#99CCCC"></FooterStyle>
<PagerStyle ForeColor="#003399" HorizontalAlign="Left"
BackColor="#99CCCC"></PagerStyle>
<HeaderStyle ForeColor="#CCCCFF" Font-Bold="True"
BackColor="#003399"></HeaderStyle>
<Columns>
<asp:BoundField DataField="ProductID" HeaderText="ProductID"
SortExpression="ProductID" />
<asp:BoundField DataField="ProductName"
HeaderText="ProductName" SortExpression="ProductName" />
<asp:BoundField DataField="QuantityPerUnit"
HeaderText="QuantityPerUnit" SortExpression="QuantityPerUnit" />
<asp:BoundField DataField="UnitPrice" HeaderText="UnitPrice"
SortExpression="UnitPrice" />
<asp:BoundField DataField="UnitsInStock"
HeaderText="UnitsInStock" SortExpression="UnitsInStock" />
</Columns>
<SelectedRowStyle ForeColor="#CCFF99" Font-Bold="True"
BackColor="#009999"></SelectedRowStyle>
<RowStyle ForeColor="#003399" BackColor="White"></RowStyle>
</asp:GridView>
</div>
</form>
</body>
</html>

Below is the code for the editing folder folder:

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<script runat="server">

</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:SqlDataSource ID="productsDataSource" Runat="server"
SelectCommand="SELECT dbo.Products.ProductID, dbo.Products.ProductName,
dbo.Products.CategoryID, dbo.Categories.CategoryName FROM dbo.Products INNER
JOIN dbo.Categories ON dbo.Products.CategoryID = dbo.Categories.CategoryID"
UpdateCommand="UPDATE [Products] SET [ProductName] =
@ProductName, [CategoryID] = @CategoryID WHERE [ProductID] =
@original_ProductID"
ConnectionString="<%$ ConnectionStrings:NWConnectionString %>">
<UpdateParameters>
<asp:Parameter Type="String"
Name="ProductName"></asp:Parameter>
<asp:Parameter Type="Int32" Name="CategoryID"></asp:Parameter>
<asp:Parameter Name="original_ProductID"></asp:Parameter>
</UpdateParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="categoryDataSource" Runat="server"
SelectCommand="SELECT [CategoryID], [CategoryName] FROM [Categories] ORDER BY
[CategoryName]"
ConnectionString="<%$ ConnectionStrings:NWConnectionString %>">
</asp:SqlDataSource>

<asp:GridView ID="GridView1" Runat="server"
DataSourceID="productsDataSource" DataKeyNames="ProductID"
AutoGenerateColumns="False" AllowPaging="True" BorderWidth="1px"
BackColor="White"
CellPadding="3" BorderStyle="None" BorderColor="#CCCCCC">
<FooterStyle ForeColor="#000066" BackColor="White"></FooterStyle>
<PagerStyle ForeColor="#000066" HorizontalAlign="Left"
BackColor="White"></PagerStyle>
<HeaderStyle ForeColor="White" Font-Bold="True"
BackColor="#006699"></HeaderStyle>
<Columns>
<asp:CommandField ShowEditButton="True"></asp:CommandField>
<asp:BoundField ReadOnly="True" HeaderText="ProductID"
InsertVisible="False" DataField="ProductID"
SortExpression="ProductID">
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:BoundField>
<asp:BoundField HeaderText="ProductName"
DataField="ProductName" SortExpression="ProductName"></asp:BoundField>
<asp:TemplateField SortExpression="CategoryName"
HeaderText="CategoryName"><EditItemTemplate>
<asp:DropDownList ID="DropDownList1" Runat="server"
DataSourceID="categoryDataSource"
DataTextField="CategoryName"
DataValueField="CategoryID" SelectedValue='<%# Bind("CategoryID") %>'>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label Runat="server" Text='<%#
Bind("CategoryName") %>' ID="Label1"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<SelectedRowStyle ForeColor="White" Font-Bold="True"
BackColor="#669999"></SelectedRowStyle>
<RowStyle ForeColor="#000066"></RowStyle>
</asp:GridView>

</div>
</form>
</body>
</html>

Below is the code for the CustomizingEditingUI


%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<script runat="server">

</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:SqlDataSource ID="productsDataSource" Runat="server"
SelectCommand="SELECT dbo.Products.ProductID, dbo.Products.ProductName,
dbo.Products.CategoryID, dbo.Categories.CategoryName FROM dbo.Products INNER
JOIN dbo.Categories ON dbo.Products.CategoryID = dbo.Categories.CategoryID"
UpdateCommand="UPDATE [Products] SET [ProductName] =
@ProductName, [CategoryID] = @CategoryID WHERE [ProductID] =
@original_ProductID"
ConnectionString="<%$ ConnectionStrings:NWConnectionString %>">
<UpdateParameters>
<asp:Parameter Type="String"
Name="ProductName"></asp:Parameter>
<asp:Parameter Type="Int32" Name="CategoryID"></asp:Parameter>
<asp:Parameter Name="original_ProductID"></asp:Parameter>
</UpdateParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="categoryDataSource" Runat="server"
SelectCommand="SELECT [CategoryID], [CategoryName] FROM [Categories] ORDER BY
[CategoryName]"
ConnectionString="<%$ ConnectionStrings:NWConnectionString %>">
</asp:SqlDataSource>

<asp:GridView ID="GridView1" Runat="server"
DataSourceID="productsDataSource" DataKeyNames="ProductID"
AutoGenerateColumns="False" AllowPaging="True" BorderWidth="1px"
BackColor="White"
CellPadding="3" BorderStyle="None" BorderColor="#CCCCCC">
<FooterStyle ForeColor="#000066" BackColor="White"></FooterStyle>
<PagerStyle ForeColor="#000066" HorizontalAlign="Left"
BackColor="White"></PagerStyle>
<HeaderStyle ForeColor="White" Font-Bold="True"
BackColor="#006699"></HeaderStyle>
<Columns>
<asp:CommandField ShowEditButton="True"></asp:CommandField>
<asp:BoundField ReadOnly="True" HeaderText="ProductID"
InsertVisible="False" DataField="ProductID"
SortExpression="ProductID">
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:BoundField>
<asp:BoundField HeaderText="ProductName"
DataField="ProductName" SortExpression="ProductName"></asp:BoundField>
<asp:TemplateField SortExpression="CategoryName"
HeaderText="CategoryName"><EditItemTemplate>
<asp:DropDownList ID="DropDownList1" Runat="server"
DataSourceID="categoryDataSource"
DataTextField="CategoryName"
DataValueField="CategoryID" SelectedValue='<%# Bind("CategoryID") %>'>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label Runat="server" Text='<%#
Bind("CategoryName") %>' ID="Label1"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<SelectedRowStyle ForeColor="White" Font-Bold="True"
BackColor="#669999"></SelectedRowStyle>
<RowStyle ForeColor="#000066"></RowStyle>
</asp:GridView>

</div>
</form>
</body>
</html>
 
Hi Alex,

Thanks for your code. Could you clarify what do you mean by this?

Quote from Alex==================================================
However the product name field is
showing the same value.
==================================================

And what's the expected layout?

Regards,
Allen Chen
Microsoft Online Support
 
Allen,

When I run the code, the gridview column for productname also shows the same
value in all instances.

Thanks.
 
Hi Alex,

Is my understanding correct that you mean you see the same value in the
ProductName column on each row but other columns are fine? If so it's
really strange because the code you provided seems has no problem.

Could you send me a demo project that can reproduce this problem? My email
is (e-mail address removed) update here after sending the project in
case I missed that email.

Regards,
Allen Chen
Microsoft Online Community Support
 
Allen,

I have been able to make things work for for the productname fields and the
Default.aspx page from the editing folder is working for updating all the
columns. The problem I am having now is that pages for the
CustomizingEditingUI folder is not working. What feature is this folder
intended to demonstrate. Also I am gettting the error message below for the
default.aspx page for the validation folder. I am emailing you a zipped file
of the solution file next as requested. Check your inbox and this is the
error message I am getting

Must declare the scalar variable "@original_ProductID".

Thanks
Alex


Alex. O. Koranteng said:
Allen,

Below is the code for the displaying data page:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<script runat="server">

</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:ObjectDataSource ID="productDataSource" Runat="server"
TypeName="ProductDAL"
SelectMethod="GetProducts" EnablePaging="True"
SelectCountMethod="TotalNumberOfProducts" UpdateMethod="UpdateProduct">
<UpdateParameters>
<asp:Parameter Name="ProductID" Type="Int32" />
<asp:Parameter Name="productName" Type="String" />
<asp:Parameter Name="unitPrice" Type="Decimal" />
<asp:Parameter Name="unitsInStock" Type="Int32" />
</UpdateParameters>
</asp:ObjectDataSource>
<asp:GridView ID="GridView1" Runat="server"
DataSourceID="productDataSource"
AutoGenerateColumns="False" AllowPaging="True" BorderWidth="1px"
BackColor="White"
CellPadding="4" BorderStyle="None" BorderColor="#3366CC">
<FooterStyle ForeColor="#003399"
BackColor="#99CCCC"></FooterStyle>
<PagerStyle ForeColor="#003399" HorizontalAlign="Left"
BackColor="#99CCCC"></PagerStyle>
<HeaderStyle ForeColor="#CCCCFF" Font-Bold="True"
BackColor="#003399"></HeaderStyle>
<Columns>
<asp:BoundField DataField="ProductID" HeaderText="ProductID"
SortExpression="ProductID" />
<asp:BoundField DataField="ProductName"
HeaderText="ProductName" SortExpression="ProductName" />
<asp:BoundField DataField="QuantityPerUnit"
HeaderText="QuantityPerUnit" SortExpression="QuantityPerUnit" />
<asp:BoundField DataField="UnitPrice" HeaderText="UnitPrice"
SortExpression="UnitPrice" />
<asp:BoundField DataField="UnitsInStock"
HeaderText="UnitsInStock" SortExpression="UnitsInStock" />
</Columns>
<SelectedRowStyle ForeColor="#CCFF99" Font-Bold="True"
BackColor="#009999"></SelectedRowStyle>
<RowStyle ForeColor="#003399" BackColor="White"></RowStyle>
</asp:GridView>
</div>
</form>
</body>
</html>

Below is the code for the editing folder folder:

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<script runat="server">

</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:SqlDataSource ID="productsDataSource" Runat="server"
SelectCommand="SELECT dbo.Products.ProductID, dbo.Products.ProductName,
dbo.Products.CategoryID, dbo.Categories.CategoryName FROM dbo.Products INNER
JOIN dbo.Categories ON dbo.Products.CategoryID = dbo.Categories.CategoryID"
UpdateCommand="UPDATE [Products] SET [ProductName] =
@ProductName, [CategoryID] = @CategoryID WHERE [ProductID] =
@original_ProductID"
ConnectionString="<%$ ConnectionStrings:NWConnectionString %>">
<UpdateParameters>
<asp:Parameter Type="String"
Name="ProductName"></asp:Parameter>
<asp:Parameter Type="Int32" Name="CategoryID"></asp:Parameter>
<asp:Parameter Name="original_ProductID"></asp:Parameter>
</UpdateParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="categoryDataSource" Runat="server"
SelectCommand="SELECT [CategoryID], [CategoryName] FROM [Categories] ORDER BY
[CategoryName]"
ConnectionString="<%$ ConnectionStrings:NWConnectionString %>">
</asp:SqlDataSource>

<asp:GridView ID="GridView1" Runat="server"
DataSourceID="productsDataSource" DataKeyNames="ProductID"
AutoGenerateColumns="False" AllowPaging="True" BorderWidth="1px"
BackColor="White"
CellPadding="3" BorderStyle="None" BorderColor="#CCCCCC">
<FooterStyle ForeColor="#000066" BackColor="White"></FooterStyle>
<PagerStyle ForeColor="#000066" HorizontalAlign="Left"
BackColor="White"></PagerStyle>
<HeaderStyle ForeColor="White" Font-Bold="True"
BackColor="#006699"></HeaderStyle>
<Columns>
<asp:CommandField ShowEditButton="True"></asp:CommandField>
<asp:BoundField ReadOnly="True" HeaderText="ProductID"
InsertVisible="False" DataField="ProductID"
SortExpression="ProductID">
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:BoundField>
<asp:BoundField HeaderText="ProductName"
DataField="ProductName" SortExpression="ProductName"></asp:BoundField>
<asp:TemplateField SortExpression="CategoryName"
HeaderText="CategoryName"><EditItemTemplate>
<asp:DropDownList ID="DropDownList1" Runat="server"
DataSourceID="categoryDataSource"
DataTextField="CategoryName"
DataValueField="CategoryID" SelectedValue='<%# Bind("CategoryID") %>'>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label Runat="server" Text='<%#
Bind("CategoryName") %>' ID="Label1"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<SelectedRowStyle ForeColor="White" Font-Bold="True"
BackColor="#669999"></SelectedRowStyle>
<RowStyle ForeColor="#000066"></RowStyle>
</asp:GridView>

</div>
</form>
</body>
</html>

Below is the code for the CustomizingEditingUI


%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<script runat="server">

</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:SqlDataSource ID="productsDataSource" Runat="server"
SelectCommand="SELECT dbo.Products.ProductID, dbo.Products.ProductName,
dbo.Products.CategoryID, dbo.Categories.CategoryName FROM dbo.Products INNER
JOIN dbo.Categories ON dbo.Products.CategoryID = dbo.Categories.CategoryID"
UpdateCommand="UPDATE [Products] SET [ProductName] =
@ProductName, [CategoryID] = @CategoryID WHERE [ProductID] =
@original_ProductID"
ConnectionString="<%$ ConnectionStrings:NWConnectionString %>">
<UpdateParameters>
<asp:Parameter Type="String"
Name="ProductName"></asp:Parameter>
<asp:Parameter Type="Int32" Name="CategoryID"></asp:Parameter>
<asp:Parameter Name="original_ProductID"></asp:Parameter>
</UpdateParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="categoryDataSource" Runat="server"
SelectCommand="SELECT [CategoryID], [CategoryName] FROM [Categories] ORDER BY
[CategoryName]"
ConnectionString="<%$ ConnectionStrings:NWConnectionString %>">
</asp:SqlDataSource>

<asp:GridView ID="GridView1" Runat="server"
DataSourceID="productsDataSource" DataKeyNames="ProductID"
AutoGenerateColumns="False" AllowPaging="True" BorderWidth="1px"
BackColor="White"
CellPadding="3" BorderStyle="None" BorderColor="#CCCCCC">
<FooterStyle ForeColor="#000066" BackColor="White"></FooterStyle>
<PagerStyle ForeColor="#000066" HorizontalAlign="Left"
BackColor="White"></PagerStyle>
<HeaderStyle ForeColor="White" Font-Bold="True"
BackColor="#006699"></HeaderStyle>
<Columns>
<asp:CommandField ShowEditButton="True"></asp:CommandField>
<asp:BoundField ReadOnly="True" HeaderText="ProductID"
InsertVisible="False" DataField="ProductID"
SortExpression="ProductID">
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:BoundField>
<asp:BoundField HeaderText="ProductName"
DataField="ProductName" SortExpression="ProductName"></asp:BoundField>
<asp:TemplateField SortExpression="CategoryName"
HeaderText="CategoryName"><EditItemTemplate>
<asp:DropDownList ID="DropDownList1" Runat="server"
DataSourceID="categoryDataSource"
DataTextField="CategoryName"
DataValueField="CategoryID" SelectedValue='<%# Bind("CategoryID") %>'>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label Runat="server" Text='<%#
Bind("CategoryName") %>' ID="Label1"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<SelectedRowStyle ForeColor="White" Font-Bold="True"
BackColor="#669999"></SelectedRowStyle>
<RowStyle ForeColor="#000066"></RowStyle>
</asp:GridView>

</div>
</form>
</body>
</html>












Allen Chen said:
Hi Alex,

My name is Allen Chen. It's my pleasure to work with you on this issue.

First, could you clarify what do you mean by this?

Quote from Alex==================================================
However the product name field is
showing the same value.
==================================================

Could you provide your current aspx code and tell me your requirement in
detail?

Regards,
Allen Chen
Microsoft Online Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 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. 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/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hi Alex,

I've received the project. Thanks.

The problem is that I cannot find the database in the project so I cannot
reproduce this problem.

For now I could only guess from the code you provided. The problem occurs
when you update the GridView on the Default.aspx of the DataBoundDDl
folder, right?

If so you can try to add OldValuesParameterFormatString in the Default.aspx
to see if it works:

<asp:SqlDataSource ID="productsDataSource"
OldValuesParameterFormatString="original_{0}"

If you still get the exception please send me a project along with the
database and tell me how to reproduce this problem so that I can test it on
my side.

Regards,
Allen Chen
Microsoft Online Community Support
 
Allen,

The database is good old northwind. I will implement your suggestion in the
meantime and test the page again. In the meantime you can check it out on
your end and thanks for all your help soo far.
 
Hi Alex,

Thanks for your information. I've tested the Default.aspx in the
DataBoundDDl folder and see the same exception when updating. My guess is
correct that it can be fixed by adding
OldValuesParameterFormatString="original_{0}". You can try it to see if it
works.

Regards,
Allen Chen
Microsoft Online Community Support
 
Allen,

I will implement the code changes you suggested and update you by the end of
day. Thanks for your support.
 
Hi Alex,,

Have you tested my code? What's the result?

Regards,
Allen Chen
Microsoft Online Support
 
Back
Top