Connecting Tech Pros Worldwide Help | Site Map

Datagrid example

Mark B
Guest
 
Posts: n/a
#1: Jun 27 '08
Can someone write some VB.Net example code for me that does this:

1) Creates a gridview control with the results of a SQL stored procedure
that has 1 parameter (text)

2) Adds an extra column that displays the result of a VB.Net function that
uses a value from an existing column

I have spent 4 hours trying to do this after Googling for examples... There
are many new concepts that I haven't had much experience with...

Steven Cheng [MSFT]
Guest
 
Posts: n/a
#2: Jun 27 '08

re: Datagrid example


Hi Mark,

Regarding on the questions you mentioned, here are some of my suggestion:

1) Creates a gridview control with the results of a SQL stored procedure
that has 1 parameter (text)
==============================
I'm wondering what's the difficulty you encounter here. To display the
results, I think it include two parts:

** first, you need to execute the SQSL stored procedure and get the
returned resultset(need to supply the parameter)

**second, you should bind the resultset to the gridview control

for calling stored procedure in ASP.NET, here are many reference:

#How to call SQL Server stored procedures in ASP.NET by using Visual Basic
.NET
http://support.microsoft.com/kb/306574

And you can also use SQL Datasource control in ASP.NET 2.0 which make
databinding/query more convenient:

#SQLDataSource Control
http://www.learn-asp.net/ASPTutorial...ceControl.aspx

http://www.asp.net/LEARN/data-access...ial-47-cs.aspx


For bind resultset to Gridview, I think it's quite straightforward:

** if you query the database programmtically, you need to assign the result
set(datatable or datareader) to Gridview and call DataBind method. e.g.

Gridview1.DataSource = [returned data resultset];
Gridview1.DataBind();

Or you can use SqlDataSource control(refer to the aritlce I mentioned
above).



2) Adds an extra column that displays the result of a VB.Net function that
uses a value from an existing column
================================
I think the natural approach is adding a template column which use other
existing column's datafield to compute the certain value you want. Here are
some reference about using template column:

#Using TemplateFields in the GridView Control
http://www.asp.net/learn/data-access...ial-12-cs.aspx

#GridView Examples for ASP.NET 2.0: Working with TemplateFields
http://msdn.microsoft.com/en-us/library/aa479353.aspx


And here is a simple example. I use a gridview to display data through a
SqlDataSource control and it contains two columns by default. I add an
additional template column which use a helper function(to calculate the
display value depend on the two existing column value):
Quote:
Quote:
Quote:
>>>>>>>>>>aspx template>>>>>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:testdbConnectionString
%>"
SelectCommand="SELECT [id], [name] FROM
[numtable]"></asp:SqlDataSource>
<br />
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False"
DataKeyNames="id" DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="id" HeaderText="id"
ReadOnly="True"
SortExpression="id" />
<asp:BoundField DataField="name" HeaderText="name"
SortExpression="name" />
<asp:TemplateField>
<ItemTemplate>
<%# MyFunction(Eval("id"), Eval("name")) %>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Quote:
Quote:
Quote:
>>>>>>>>code behind>>>>>>>>>>>>>>>
protected string MyFunction(object id, object name)
{
return id.ToString() + "|" + name.ToString();
}
Quote:
Quote:
Quote:
>>>>>>>>>>>>>>>>>>>>>>
In addition, if you do not have much ASP.NET programming experience, I
would suggest you have a look at the tutorials on ASP.NET offical site:

http://www.asp.net/learn/

and since you're developing data access application, the "Data Access
tutorial" is especially important for you:

http://www.asp.net/learn/data-access/

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


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:
msdnmg@microsoft.com.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

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.

--------------------
Quote:
>From: "Mark B" <none@none.com>
>Subject: Datagrid example
>Date: Fri, 30 May 2008 13:10:32 +1200
Quote:
>
>Can someone write some VB.Net example code for me that does this:
>
>1) Creates a gridview control with the results of a SQL stored procedure
>that has 1 parameter (text)
>
>2) Adds an extra column that displays the result of a VB.Net function that
>uses a value from an existing column
>
>I have spent 4 hours trying to do this after Googling for examples...
There
Quote:
>are many new concepts that I haven't had much experience with...
>
>
Steven Cheng [MSFT]
Guest
 
Posts: n/a
#3: Jun 27 '08

re: Datagrid example


Hi Mark,

Do you have any further questions on this or does the suggestion in my last
reply help you some?

Sincerely,

Steven Cheng
Microsoft MSDN Online Support Lead


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:
msdnmg@microsoft.com.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.



--------------------
Quote:
>From: stcheng@online.microsoft.com (Steven Cheng [MSFT])
>Organization: Microsoft
>Date: Fri, 30 May 2008 03:52:12 GMT
>Subject: RE: Datagrid example
Quote:
>
>Hi Mark,
>
>Regarding on the questions you mentioned, here are some of my suggestion:
>
>1) Creates a gridview control with the results of a SQL stored procedure
>that has 1 parameter (text)
>==============================
>I'm wondering what's the difficulty you encounter here. To display the
>results, I think it include two parts:
>
>** first, you need to execute the SQSL stored procedure and get the
>returned resultset(need to supply the parameter)
>
>**second, you should bind the resultset to the gridview control
>
>for calling stored procedure in ASP.NET, here are many reference:
>
>#How to call SQL Server stored procedures in ASP.NET by using Visual Basic
>.NET
>http://support.microsoft.com/kb/306574
>
>And you can also use SQL Datasource control in ASP.NET 2.0 which make
>databinding/query more convenient:
>
>#SQLDataSource Control
>http://www.learn-asp.net/ASPTutorial...ceControl.aspx
>
>http://www.asp.net/LEARN/data-access...ial-47-cs.aspx
>
>
>For bind resultset to Gridview, I think it's quite straightforward:
>
>** if you query the database programmtically, you need to assign the
result
Quote:
>set(datatable or datareader) to Gridview and call DataBind method. e.g.
>
>Gridview1.DataSource = [returned data resultset];
>Gridview1.DataBind();
>
>Or you can use SqlDataSource control(refer to the aritlce I mentioned
>above).
>
>
>
>2) Adds an extra column that displays the result of a VB.Net function that
>uses a value from an existing column
>================================
>I think the natural approach is adding a template column which use other
>existing column's datafield to compute the certain value you want. Here
are
Quote:
>some reference about using template column:
>
>#Using TemplateFields in the GridView Control
>http://www.asp.net/learn/data-access...ial-12-cs.aspx
>
>#GridView Examples for ASP.NET 2.0: Working with TemplateFields
>http://msdn.microsoft.com/en-us/library/aa479353.aspx
>
>
>And here is a simple example. I use a gridview to display data through a
>SqlDataSource control and it contains two columns by default. I add an
>additional template column which use a helper function(to calculate the
>display value depend on the two existing column value):
>
Quote:
Quote:
>>>>>>>>>>>aspx template>>>>>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:testdbConnectionString
>%>"
SelectCommand="SELECT [id], [name] FROM
>[numtable]"></asp:SqlDataSource>
<br />
<asp:GridView ID="GridView1" runat="server"
>AutoGenerateColumns="False"
DataKeyNames="id" DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="id" HeaderText="id"
>ReadOnly="True"
SortExpression="id" />
<asp:BoundField DataField="name" HeaderText="name"
>SortExpression="name" />
<asp:TemplateField>
<ItemTemplate>
<%# MyFunction(Eval("id"), Eval("name")) %>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
>
Quote:
Quote:
>>>>>>>>>code behind>>>>>>>>>>>>>>>
>protected string MyFunction(object id, object name)
{
return id.ToString() + "|" + name.ToString();
}
>
Quote:
Quote:
>>>>>>>>>>>>>>>>>>>>>>>
>
>In addition, if you do not have much ASP.NET programming experience, I
>would suggest you have a look at the tutorials on ASP.NET offical site:
>
>http://www.asp.net/learn/
>
>and since you're developing data access application, the "Data Access
>tutorial" is especially important for you:
>
>http://www.asp.net/learn/data-access/
>
>Sincerely,
>
>Steven Cheng
>
>Microsoft MSDN Online Support Lead
>
>
>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:
>msdnmg@microsoft.com.
>
>================================================= =
>Get notification to my posts through email? Please refer to
>http://msdn.microsoft.com/subscripti...ault.aspx#noti
f
Quote:
>ications.
>
>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.
>
>--------------------
Quote:
>>From: "Mark B" <none@none.com>
>>Subject: Datagrid example
>>Date: Fri, 30 May 2008 13:10:32 +1200
>
Quote:
>>
>>Can someone write some VB.Net example code for me that does this:
>>
>>1) Creates a gridview control with the results of a SQL stored procedure
>>that has 1 parameter (text)
>>
>>2) Adds an extra column that displays the result of a VB.Net function
that
Quote:
Quote:
>>uses a value from an existing column
>>
>>I have spent 4 hours trying to do this after Googling for examples...
>There
Quote:
>>are many new concepts that I haven't had much experience with...
>>
>>
>
>
Mark B
Guest
 
Posts: n/a
#4: Jun 27 '08

re: Datagrid example


Thanks Steven. I am planning to start that reading you suggested right now.

Thanks for the offer of answering any further questions I have.



"Steven Cheng [MSFT]" <stcheng@online.microsoft.comwrote in message
news:Hfo15XUxIHA.1784@TK2MSFTNGHUB02.phx.gbl...
Quote:
Hi Mark,
>
Do you have any further questions on this or does the suggestion in my
last
reply help you some?
>
Sincerely,
>
Steven Cheng
Microsoft MSDN Online Support Lead
>
>
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:
msdnmg@microsoft.com.
>
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.
>
>
>
--------------------
Quote:
>>From: stcheng@online.microsoft.com (Steven Cheng [MSFT])
>>Organization: Microsoft
>>Date: Fri, 30 May 2008 03:52:12 GMT
>>Subject: RE: Datagrid example
>
Quote:
>>
>>Hi Mark,
>>
>>Regarding on the questions you mentioned, here are some of my suggestion:
>>
>>1) Creates a gridview control with the results of a SQL stored procedure
>>that has 1 parameter (text)
>>==============================
>>I'm wondering what's the difficulty you encounter here. To display the
>>results, I think it include two parts:
>>
>>** first, you need to execute the SQSL stored procedure and get the
>>returned resultset(need to supply the parameter)
>>
>>**second, you should bind the resultset to the gridview control
>>
>>for calling stored procedure in ASP.NET, here are many reference:
>>
>>#How to call SQL Server stored procedures in ASP.NET by using Visual Basic
>>.NET
>>http://support.microsoft.com/kb/306574
>>
>>And you can also use SQL Datasource control in ASP.NET 2.0 which make
>>databinding/query more convenient:
>>
>>#SQLDataSource Control
>>http://www.learn-asp.net/ASPTutorial...ceControl.aspx
>>
>>http://www.asp.net/LEARN/data-access...ial-47-cs.aspx
>>
>>
>>For bind resultset to Gridview, I think it's quite straightforward:
>>
>>** if you query the database programmtically, you need to assign the
result
Quote:
>>set(datatable or datareader) to Gridview and call DataBind method. e.g.
>>
>>Gridview1.DataSource = [returned data resultset];
>>Gridview1.DataBind();
>>
>>Or you can use SqlDataSource control(refer to the aritlce I mentioned
>>above).
>>
>>
>>
>>2) Adds an extra column that displays the result of a VB.Net function that
>>uses a value from an existing column
>>================================
>>I think the natural approach is adding a template column which use other
>>existing column's datafield to compute the certain value you want. Here
are
Quote:
>>some reference about using template column:
>>
>>#Using TemplateFields in the GridView Control
>>http://www.asp.net/learn/data-access...ial-12-cs.aspx
>>
>>#GridView Examples for ASP.NET 2.0: Working with TemplateFields
>>http://msdn.microsoft.com/en-us/library/aa479353.aspx
>>
>>
>>And here is a simple example. I use a gridview to display data through a
>>SqlDataSource control and it contains two columns by default. I add an
>>additional template column which use a helper function(to calculate the
>>display value depend on the two existing column value):
>>
Quote:
>>>>>>>>>>>>aspx template>>>>>
><asp:SqlDataSource ID="SqlDataSource1" runat="server"
> ConnectionString="<%$ ConnectionStrings:testdbConnectionString
>>%>"
> SelectCommand="SELECT [id], [name] FROM
>>[numtable]"></asp:SqlDataSource>
> <br />
> <asp:GridView ID="GridView1" runat="server"
>>AutoGenerateColumns="False"
> DataKeyNames="id" DataSourceID="SqlDataSource1">
> <Columns>
> <asp:BoundField DataField="id" HeaderText="id"
>>ReadOnly="True"
> SortExpression="id" />
> <asp:BoundField DataField="name" HeaderText="name"
>>SortExpression="name" />
> <asp:TemplateField>
> <ItemTemplate>
> <%# MyFunction(Eval("id"), Eval("name")) %>
> </ItemTemplate>
> </asp:TemplateField>
> </Columns>
> </asp:GridView>
>>
Quote:
>>>>>>>>>>code behind>>>>>>>>>>>>>>>
>>protected string MyFunction(object id, object name)
> {
> return id.ToString() + "|" + name.ToString();
> }
>>
Quote:
>>>>>>>>>>>>>>>>>>>>>>>>
>>
>>In addition, if you do not have much ASP.NET programming experience, I
>>would suggest you have a look at the tutorials on ASP.NET offical site:
>>
>>http://www.asp.net/learn/
>>
>>and since you're developing data access application, the "Data Access
>>tutorial" is especially important for you:
>>
>>http://www.asp.net/learn/data-access/
>>
>>Sincerely,
>>
>>Steven Cheng
>>
>>Microsoft MSDN Online Support Lead
>>
>>
>>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:
>>msdnmg@microsoft.com.
>>
>>================================================ ==
>>Get notification to my posts through email? Please refer to
>>http://msdn.microsoft.com/subscripti...ault.aspx#noti
f
Quote:
>>ications.
>>
>>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.
>>
>>--------------------
Quote:
>>>From: "Mark B" <none@none.com>
>>>Subject: Datagrid example
>>>Date: Fri, 30 May 2008 13:10:32 +1200
>>
Quote:
>>>
>>>Can someone write some VB.Net example code for me that does this:
>>>
>>>1) Creates a gridview control with the results of a SQL stored procedure
>>>that has 1 parameter (text)
>>>
>>>2) Adds an extra column that displays the result of a VB.Net function
that
Quote:
Quote:
>>>uses a value from an existing column
>>>
>>>I have spent 4 hours trying to do this after Googling for examples...
>>There
Quote:
>>>are many new concepts that I haven't had much experience with...
>>>
>>>
>>
>>
>
Mark B
Guest
 
Posts: n/a
#5: Jun 27 '08

re: Datagrid example


When I drag the Datagrid control over to the design view of an aspx page, it
prompts me for many things like datasource, fields etc. After I fill all of
these in I notice it creates quite a lot of html:

e.g.

<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:MyDBConnectionString %>"
SelectCommand="uspGroupsDataGridSource"
SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:QueryStringParameter DefaultValue="Group1"
Name="EnterMasterGroupName"
QueryStringField="SelectedGroup" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False" CellPadding="4"
DataSourceID="SqlDataSource1" ForeColor="#333333" GridLines="None">
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White"
/>
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<Columns>
<asp:BoundField DataField="GroupName" HeaderText="GroupName"
SortExpression="GroupName" />
<asp:BoundField DataField="AverageSales30Days"
HeaderText="AverageSales30Days"
ReadOnly="True" SortExpression="AverageSales30Days" />
<asp:BoundField DataField="AverageGrade30Days"
HeaderText="AverageGrade30Days"
ReadOnly="True" SortExpression="AverageGrade30Days" />
<asp:BoundField DataField="AverageSales6Months"
HeaderText="AverageSales6Months"
ReadOnly="True" SortExpression="AverageSales6Months" />
<asp:BoundField DataField="AverageGrade6Months"
HeaderText="AverageGrade6Months" ReadOnly="True"
SortExpression="AverageGrade6Months" />
<asp:BoundField DataField="AverageSales12Months"
HeaderText="AverageSales12Months"
ReadOnly="True" SortExpression="AverageSales12Months" />
<asp:BoundField DataField="AverageGrade12Months"
HeaderText="AverageGrade12Months" ReadOnly="True"
SortExpression="AverageGrade12Months" />
</Columns>
<PagerStyle BackColor="#284775" ForeColor="White"
HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True"
ForeColor="#333333" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White"
/>
<EditRowStyle BackColor="#999999" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>




So my question is, if if follow the example below and type in VB.Net code,
will this conflict with what I already have with the HTML? Which one will
take precedence?

Do I need to delete some of the HTML?




"Mark B" <none@none.comwrote in message
news:uBxmBkfxIHA.704@TK2MSFTNGP05.phx.gbl...
Quote:
Thanks Steven. I am planning to start that reading you suggested right
now.
>
Thanks for the offer of answering any further questions I have.
>
>
>
"Steven Cheng [MSFT]" <stcheng@online.microsoft.comwrote in message
news:Hfo15XUxIHA.1784@TK2MSFTNGHUB02.phx.gbl...
Quote:
>Hi Mark,
>>
>Do you have any further questions on this or does the suggestion in my
>last
>reply help you some?
>>
>Sincerely,
>>
>Steven Cheng
>Microsoft MSDN Online Support Lead
>>
>>
>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:
>msdnmg@microsoft.com.
>>
>================================================= =
>Get notification to my posts through email? Please refer to
>http://msdn.microsoft.com/subscripti...ult.aspx#notif
>ications.
>>
>>
>>
>--------------------
Quote:
>>>From: stcheng@online.microsoft.com (Steven Cheng [MSFT])
>>>Organization: Microsoft
>>>Date: Fri, 30 May 2008 03:52:12 GMT
>>>Subject: RE: Datagrid example
>>
Quote:
>>>
>>>Hi Mark,
>>>
>>>Regarding on the questions you mentioned, here are some of my suggestion:
>>>
>>>1) Creates a gridview control with the results of a SQL stored procedure
>>>that has 1 parameter (text)
>>>==============================
>>>I'm wondering what's the difficulty you encounter here. To display the
>>>results, I think it include two parts:
>>>
>>>** first, you need to execute the SQSL stored procedure and get the
>>>returned resultset(need to supply the parameter)
>>>
>>>**second, you should bind the resultset to the gridview control
>>>
>>>for calling stored procedure in ASP.NET, here are many reference:
>>>
>>>#How to call SQL Server stored procedures in ASP.NET by using Visual
>>>Basic
>>>.NET
>>>http://support.microsoft.com/kb/306574
>>>
>>>And you can also use SQL Datasource control in ASP.NET 2.0 which make
>>>databinding/query more convenient:
>>>
>>>#SQLDataSource Control
>>>http://www.learn-asp.net/ASPTutorial...ceControl.aspx
>>>
>>>http://www.asp.net/LEARN/data-access...ial-47-cs.aspx
>>>
>>>
>>>For bind resultset to Gridview, I think it's quite straightforward:
>>>
>>>** if you query the database programmtically, you need to assign the
>result
Quote:
>>>set(datatable or datareader) to Gridview and call DataBind method. e.g.
>>>
>>>Gridview1.DataSource = [returned data resultset];
>>>Gridview1.DataBind();
>>>
>>>Or you can use SqlDataSource control(refer to the aritlce I mentioned
>>>above).
>>>
>>>
>>>
>>>2) Adds an extra column that displays the result of a VB.Net function
>>>that
>>>uses a value from an existing column
>>>================================
>>>I think the natural approach is adding a template column which use other
>>>existing column's datafield to compute the certain value you want. Here
>are
Quote:
>>>some reference about using template column:
>>>
>>>#Using TemplateFields in the GridView Control
>>>http://www.asp.net/learn/data-access...ial-12-cs.aspx
>>>
>>>#GridView Examples for ASP.NET 2.0: Working with TemplateFields
>>>http://msdn.microsoft.com/en-us/library/aa479353.aspx
>>>
>>>
>>>And here is a simple example. I use a gridview to display data through a
>>>SqlDataSource control and it contains two columns by default. I add an
>>>additional template column which use a helper function(to calculate the
>>>display value depend on the two existing column value):
>>>
>>>>>>>>>>>>>aspx template>>>>>
>><asp:SqlDataSource ID="SqlDataSource1" runat="server"
>> ConnectionString="<%$
>>ConnectionStrings:testdbConnectionString
>>>%>"
>> SelectCommand="SELECT [id], [name] FROM
>>>[numtable]"></asp:SqlDataSource>
>> <br />
>> <asp:GridView ID="GridView1" runat="server"
>>>AutoGenerateColumns="False"
>> DataKeyNames="id" DataSourceID="SqlDataSource1">
>> <Columns>
>> <asp:BoundField DataField="id" HeaderText="id"
>>>ReadOnly="True"
>> SortExpression="id" />
>> <asp:BoundField DataField="name" HeaderText="name"
>>>SortExpression="name" />
>> <asp:TemplateField>
>> <ItemTemplate>
>> <%# MyFunction(Eval("id"), Eval("name")) %>
>> </ItemTemplate>
>> </asp:TemplateField>
>> </Columns>
>> </asp:GridView>
>>>
>>>>>>>>>>>code behind>>>>>>>>>>>>>>>
>>>protected string MyFunction(object id, object name)
>> {
>> return id.ToString() + "|" + name.ToString();
>> }
>>>
>>>>>>>>>>>>>>>>>>>>>>>>>
>>>
>>>In addition, if you do not have much ASP.NET programming experience, I
>>>would suggest you have a look at the tutorials on ASP.NET offical site:
>>>
>>>http://www.asp.net/learn/
>>>
>>>and since you're developing data access application, the "Data Access
>>>tutorial" is especially important for you:
>>>
>>>http://www.asp.net/learn/data-access/
>>>
>>>Sincerely,
>>>
>>>Steven Cheng
>>>
>>>Microsoft MSDN Online Support Lead
>>>
>>>
>>>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:
>>>msdnmg@microsoft.com.
>>>
>>>=============================================== ===
>>>Get notification to my posts through email? Please refer to
>>>http://msdn.microsoft.com/subscripti...ault.aspx#noti
>f
Quote:
>>>ications.
>>>
>>>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.
>>>
>>>--------------------
>>>>From: "Mark B" <none@none.com>
>>>>Subject: Datagrid example
>>>>Date: Fri, 30 May 2008 13:10:32 +1200
>>>
>>>>
>>>>Can someone write some VB.Net example code for me that does this:
>>>>
>>>>1) Creates a gridview control with the results of a SQL stored procedure
>>>>that has 1 parameter (text)
>>>>
>>>>2) Adds an extra column that displays the result of a VB.Net function
>that
Quote:
>>>>uses a value from an existing column
>>>>
>>>>I have spent 4 hours trying to do this after Googling for examples...
>>>There
>>>>are many new concepts that I haven't had much experience with...
>>>>
>>>>
>>>
>>>
>>
>
Steven Cheng [MSFT]
Guest
 
Posts: n/a
#6: Jun 27 '08

re: Datagrid example


thanks for your reply Mark,

From the aspx template snippet you provided, you 've configured the
DataGrid control to connect a SqlDataSource(which supply the data resultset
to DataGrid). Also, you've applied a certain style format and most of the
mark attribute it is for style format(such as color and font). You can
first remove the format/style so as to make the markup looks more clear.

As for the question you mentioned below:
===============
So my question is, if if follow the example below and type in VB.Net code,
will this conflict with what I already have with the HTML? Which one will
take precedence?
==============

Would you let me know the code you added? Are you adding additional
databinding code? Generally for ASP.NET 2.0, if you have already supplied
an SqlDatasource control(and correctly configure the Datasource control
properties) and attached it to the databound control(like DataGrid,
Gridview), the the databound control should be able to populate data
correctly without additional code.

Sincerely,

Steven Cheng
Microsoft MSDN Online Support Lead

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:
msdnmg@microsoft.com.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
Quote:
>From: "Mark B" <none@none.com>
>References: <u8757HfwIHA.5096@TK2MSFTNGP02.phx.gbl>
<gARxXigwIHA.5796@TK2MSFTNGHUB02.phx.gbl>
<Hfo15XUxIHA.1784@TK2MSFTNGHUB02.phx.gbl>
<uBxmBkfxIHA.704@TK2MSFTNGP05.phx.gbl>
Quote:
>In-Reply-To: <uBxmBkfxIHA.704@TK2MSFTNGP05.phx.gbl>
>Subject: Re: Datagrid example
>Date: Wed, 4 Jun 2008 16:26:46 +1200
Quote:
>
>When I drag the Datagrid control over to the design view of an aspx page,
it
Quote:
>prompts me for many things like datasource, fields etc. After I fill all
of
Quote:
>these in I notice it creates quite a lot of html:
>
>e.g.
>
><asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:MyDBConnectionString %>"
SelectCommand="uspGroupsDataGridSource"
SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:QueryStringParameter DefaultValue="Group1"
>Name="EnterMasterGroupName"
QueryStringField="SelectedGroup" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False" CellPadding="4"
DataSourceID="SqlDataSource1" ForeColor="#333333" GridLines="None">
<FooterStyle BackColor="#5D7B9D" Font-Bold="True"
ForeColor="White"
Quote:
>/>
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<Columns>
<asp:BoundField DataField="GroupName" HeaderText="GroupName"
SortExpression="GroupName" />
<asp:BoundField DataField="AverageSales30Days"
>HeaderText="AverageSales30Days"
ReadOnly="True" SortExpression="AverageSales30Days" />
<asp:BoundField DataField="AverageGrade30Days"
>HeaderText="AverageGrade30Days"
ReadOnly="True" SortExpression="AverageGrade30Days" />
<asp:BoundField DataField="AverageSales6Months"
>HeaderText="AverageSales6Months"
ReadOnly="True" SortExpression="AverageSales6Months" />
<asp:BoundField DataField="AverageGrade6Months"
HeaderText="AverageGrade6Months" ReadOnly="True"
SortExpression="AverageGrade6Months" />
<asp:BoundField DataField="AverageSales12Months"
>HeaderText="AverageSales12Months"
ReadOnly="True" SortExpression="AverageSales12Months" />
<asp:BoundField DataField="AverageGrade12Months"
HeaderText="AverageGrade12Months" ReadOnly="True"
SortExpression="AverageGrade12Months" />
</Columns>
<PagerStyle BackColor="#284775" ForeColor="White"
>HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True"
>ForeColor="#333333" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True"
ForeColor="White"
Quote:
>/>
<EditRowStyle BackColor="#999999" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>
>
>
>
>
>So my question is, if if follow the example below and type in VB.Net code,
>will this conflict with what I already have with the HTML? Which one will
>take precedence?
>
>Do I need to delete some of the HTML?
>
>
>
>
Mark B
Guest
 
Posts: n/a
#7: Jun 27 '08

re: Datagrid example


It turns out so far I have been able to do it all using the markup as you
showed so at present there is no need to use additional VB.net on it. Thanks
for your help.

"Steven Cheng [MSFT]" <stcheng@online.microsoft.comwrote in message
news:ICqSwatxIHA.4564@TK2MSFTNGHUB02.phx.gbl...
Quote:
thanks for your reply Mark,
>
From the aspx template snippet you provided, you 've configured the
DataGrid control to connect a SqlDataSource(which supply the data
resultset
to DataGrid). Also, you've applied a certain style format and most of the
mark attribute it is for style format(such as color and font). You can
first remove the format/style so as to make the markup looks more clear.
>
As for the question you mentioned below:
===============
So my question is, if if follow the example below and type in VB.Net code,
will this conflict with what I already have with the HTML? Which one will
take precedence?
==============
>
Would you let me know the code you added? Are you adding additional
databinding code? Generally for ASP.NET 2.0, if you have already supplied
an SqlDatasource control(and correctly configure the Datasource control
properties) and attached it to the databound control(like DataGrid,
Gridview), the the databound control should be able to populate data
correctly without additional code.
>
Sincerely,
>
Steven Cheng
Microsoft MSDN Online Support Lead
>
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:
msdnmg@microsoft.com.
>
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.
==================================================
This posting is provided "AS IS" with no warranties, and confers no
rights.
>
--------------------
Quote:
>>From: "Mark B" <none@none.com>
>>References: <u8757HfwIHA.5096@TK2MSFTNGP02.phx.gbl>
<gARxXigwIHA.5796@TK2MSFTNGHUB02.phx.gbl>
<Hfo15XUxIHA.1784@TK2MSFTNGHUB02.phx.gbl>
<uBxmBkfxIHA.704@TK2MSFTNGP05.phx.gbl>
Quote:
>>In-Reply-To: <uBxmBkfxIHA.704@TK2MSFTNGP05.phx.gbl>
>>Subject: Re: Datagrid example
>>Date: Wed, 4 Jun 2008 16:26:46 +1200
>
Quote:
>>
>>When I drag the Datagrid control over to the design view of an aspx page,
it
Quote:
>>prompts me for many things like datasource, fields etc. After I fill all
of
Quote:
>>these in I notice it creates quite a lot of html:
>>
>>e.g.
>>
>><asp:SqlDataSource ID="SqlDataSource1" runat="server"
> ConnectionString="<%$ ConnectionStrings:MyDBConnectionString %>"
> SelectCommand="uspGroupsDataGridSource"
> SelectCommandType="StoredProcedure">
> <SelectParameters>
> <asp:QueryStringParameter DefaultValue="Group1"
>>Name="EnterMasterGroupName"
> QueryStringField="SelectedGroup" Type="String" />
> </SelectParameters>
> </asp:SqlDataSource>
> <asp:GridView ID="GridView1" runat="server" AllowPaging="True"
> AllowSorting="True" AutoGenerateColumns="False" CellPadding="4"
> DataSourceID="SqlDataSource1" ForeColor="#333333"
>GridLines="None">
> <FooterStyle BackColor="#5D7B9D" Font-Bold="True"
ForeColor="White"
Quote:
>>/>
> <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
> <Columns>
> <asp:BoundField DataField="GroupName" HeaderText="GroupName"
> SortExpression="GroupName" />
> <asp:BoundField DataField="AverageSales30Days"
>>HeaderText="AverageSales30Days"
> ReadOnly="True" SortExpression="AverageSales30Days" />
> <asp:BoundField DataField="AverageGrade30Days"
>>HeaderText="AverageGrade30Days"
> ReadOnly="True" SortExpression="AverageGrade30Days" />
> <asp:BoundField DataField="AverageSales6Months"
>>HeaderText="AverageSales6Months"
> ReadOnly="True" SortExpression="AverageSales6Months" />
> <asp:BoundField DataField="AverageGrade6Months"
> HeaderText="AverageGrade6Months" ReadOnly="True"
> SortExpression="AverageGrade6Months" />
> <asp:BoundField DataField="AverageSales12Months"
>>HeaderText="AverageSales12Months"
> ReadOnly="True" SortExpression="AverageSales12Months" />
> <asp:BoundField DataField="AverageGrade12Months"
> HeaderText="AverageGrade12Months" ReadOnly="True"
> SortExpression="AverageGrade12Months" />
> </Columns>
> <PagerStyle BackColor="#284775" ForeColor="White"
>>HorizontalAlign="Center" />
> <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True"
>>ForeColor="#333333" />
> <HeaderStyle BackColor="#5D7B9D" Font-Bold="True"
ForeColor="White"
Quote:
>>/>
> <EditRowStyle BackColor="#999999" />
> <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
> </asp:GridView>
>>
>>
>>
>>
>>So my question is, if if follow the example below and type in VB.Net code,
>>will this conflict with what I already have with the HTML? Which one will
>>take precedence?
>>
>>Do I need to delete some of the HTML?
>>
>>
>>
>>
>
Steven Cheng [MSFT]
Guest
 
Posts: n/a
#8: Jun 27 '08

re: Datagrid example


Thanks for your reply Mark,

Yes, ASP.NET 2.0 databinding model makes some simple and common data access
scenarios very convenient to achieve. If you need any further help, welcome
to post here.

Sincerely,

Steven Cheng
Microsoft MSDN Online Support Lead


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:
msdnmg@microsoft.com.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
Quote:
>From: "Mark B" <none@none.com>
>In-Reply-To: <ICqSwatxIHA.4564@TK2MSFTNGHUB02.phx.gbl>
>Subject: Re: Datagrid example
>Date: Thu, 5 Jun 2008 22:03:25 +1200
Quote:
>It turns out so far I have been able to do it all using the markup as you
>showed so at present there is no need to use additional VB.net on it.
Thanks
Quote:
>for your help.
>
>"
Closed Thread