Community-Credit.com | NonProfitWays.com | SOAPitstop.com   Skin:   
      User: Not logged in 
Home
Newsletter Signup
XSLT Library
Latest XML Blogs
Featured Examples
Presentations
Featured Articles
Book Chapters
Training Courses
Events
NewsGroups
 
Discussions
Examples
Tutorials
Tools
Articles
Resources
Websites
 
Sign In
My Profile
My Articles
My Examples
My Favorites
My Resources
Add a Resource
Logout
 
About Me
My Blog
HeadGeek Articles
Talking Portfolio
Resume
Pictures
World Trip Pics

GridView Control in ASP.NET 2.0
Author: Unknown
Published: 7/24/2005 6:50 AM
Category: ASP.NET, Authentication, Database, Datagrid, DTD, Localization, SQL, VB.NET, WSE, XHTML, XSchema
Summary: Paging, Editing, Sorting, Filtering with no code.

 

1)GridView\CustomerOrders.aspx

<%@ Page Language="VB" AutoEventWireup="false" CompileWith="CustomerOrders.aspx.vb" ClassName="CustomerOrders_aspx" %>

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

<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" DataKeyNames="OrderID" DataSourceID="SqlDataSource1"

EmptyDataText="There are no data records to display." AutoGenerateColumns="False"

BorderWidth="1px" BackColor="LightGoldenrodYellow" GridLines="None" CellPadding="2"

BorderColor="Tan" ForeColor="Black">

<FooterStyle BackColor="Tan"></FooterStyle>

<PagerStyle ForeColor="DarkSlateBlue" HorizontalAlign="Center" BackColor="PaleGoldenrod"></PagerStyle>

<HeaderStyle Font-Bold="True" BackColor="Tan"></HeaderStyle>

<AlternatingRowStyle BackColor="PaleGoldenrod"></AlternatingRowStyle>

<Columns>

<asp:BoundField ReadOnly="True" HeaderText="OrderID" DataField="OrderID" SortExpression="OrderID"></asp:BoundField>

<asp:BoundField HeaderText="CustomerID" DataField="CustomerID" SortExpression="CustomerID"></asp:BoundField>

<asp:BoundField HeaderText="EmployeeID" DataField="EmployeeID" SortExpression="EmployeeID"></asp:BoundField>

<asp:BoundField HeaderText="OrderDate" DataField="OrderDate" SortExpression="OrderDate"></asp:BoundField>

<asp:BoundField HeaderText="RequiredDate" DataField="RequiredDate" SortExpression="RequiredDate"></asp:BoundField>

<asp:BoundField HeaderText="ShippedDate" DataField="ShippedDate" SortExpression="ShippedDate"></asp:BoundField>

<asp:BoundField HeaderText="ShipVia" DataField="ShipVia" SortExpression="ShipVia"></asp:BoundField>

<asp:BoundField HeaderText="Freight" DataField="Freight" SortExpression="Freight"></asp:BoundField>

<asp:BoundField HeaderText="ShipName" DataField="ShipName" SortExpression="ShipName"></asp:BoundField>

<asp:BoundField HeaderText="ShipAddress" DataField="ShipAddress" SortExpression="ShipAddress"></asp:BoundField>

<asp:BoundField HeaderText="ShipCity" DataField="ShipCity" SortExpression="ShipCity"></asp:BoundField>

<asp:BoundField HeaderText="ShipRegion" DataField="ShipRegion" SortExpression="ShipRegion"></asp:BoundField>

<asp:BoundField HeaderText="ShipPostalCode" DataField="ShipPostalCode" SortExpression="ShipPostalCode"></asp:BoundField>

<asp:BoundField HeaderText="ShipCountry" DataField="ShipCountry" SortExpression="ShipCountry"></asp:BoundField>

</Columns>

<SelectedRowStyle ForeColor="GhostWhite" BackColor="DarkSlateBlue"></SelectedRowStyle>

</asp:GridView>

<asp:SqlDataSource ID="SqlDataSource1" Runat="server" SelectCommand="SELECT * FROM [Orders] WHERE ([CustomerID] = @CustomerID)"

ConnectionString="<%$ ConnectionStrings:AppConnectionString1 %>">

<SelectParameters>

<asp:QueryStringParameter Name="CustomerID" QueryStringField="CustomerID" Type="String"></asp:QueryStringParameter>

</SelectParameters>

</asp:SqlDataSource>

</div>

</form>

</body>

</html>

2)GridView\CustomerOrders.aspx.vb

Partial Class CustomerOrders_aspx

End Class

3)GridView\Default.aspx

<%@ Page Language="VB" AutoEventWireup="false" CompileWith="Default.aspx.vb" ClassName="Default_aspx" %>

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

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

<title>Untitled Page</title>

</head>

<body>

<form id="form1" runat="server">

<div>

 

<asp:Panel ID="Panel1" Runat="server">

<asp:DropDownList ID="DropDownList1" Runat="server" DataSourceID="SqlDataSource2"

AutoPostBack="True" DataTextField="Country" DataValueField="Country" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">

</asp:DropDownList>

<asp:Button ID="btnAddCustomer" Runat="server" Text="Add Customer" OnClick="btnAddCustomer_Click" />

</asp:Panel>

<asp:GridView ID="GridView1" Runat="server" DataKeyNames="CustomerID" DataSourceID="SqlDataSource1"

EmptyDataText="There are no data records to display." AutoGenerateColumns="False"

AllowPaging="True" AllowSorting="True" BorderWidth="1px" BackColor="White" GridLines="Horizontal"

CellPadding="3" BorderStyle="None" BorderColor="#E7E7FF" OnRowCreated="GridView1_RowCreated"

OnRowEditing="GridView1_RowEditing" OnSelectedIndexChanging="GridView1_SelectedIndexChanging"

OnRowDeleted="GridView1_RowDeleted" OnDataBound="GridView1_DataBound">

<FooterStyle ForeColor="#4A3C8C" BackColor="#B5C7DE"></FooterStyle>

<PagerStyle ForeColor="#4A3C8C" HorizontalAlign="Right" BackColor="#E7E7FF"></PagerStyle>

<HeaderStyle ForeColor="#F7F7F7" Font-Bold="True" BackColor="#4A3C8C"></HeaderStyle>

<AlternatingRowStyle BackColor="#F7F7F7"></AlternatingRowStyle>

<Columns>

<asp:CommandField ShowSelectButton="True"></asp:CommandField>

<asp:BoundField ReadOnly="True" HeaderText="CustomerID" DataField="CustomerID" SortExpression="CustomerID"></asp:BoundField>

<asp:TemplateField HeaderText="CompanyName"><EditItemTemplate>

<asp:TextBox ID="TextBox1" Runat="server" Text='<%# Bind("CompanyName") %>'></asp:TextBox>

</EditItemTemplate>

<ItemTemplate>

<asp:HyperLink Runat="server" Text='<%# Eval("CompanyName") %>' NavigateUrl='<%# Eval("CustomerID", "CustomerOrders.aspx?CustomerID={0}") %>'

ID="HyperLink1"></asp:HyperLink>

</ItemTemplate>

</asp:TemplateField>

<asp:BoundField HeaderText="ContactName" DataField="ContactName" SortExpression="ContactName"></asp:BoundField>

<asp:TemplateField SortExpression="Country" HeaderText="Country"><EditItemTemplate>

<asp:DropDownList ID="DropDownList2" Runat="server" DataSourceID="SqlDataSource2"

DataTextField="Country" DataValueField="Country" SelectedValue='<%# Bind("Country") %>'>

</asp:DropDownList>

</EditItemTemplate>

<ItemTemplate>

<asp:Label Runat="server" Text='<%# Bind("Country") %>' ID="Label1"></asp:Label>

</ItemTemplate>

</asp:TemplateField>

<asp:TemplateField HeaderText="Delete"><ItemTemplate>

<asp:LinkButton ID="lnkDelete" Runat="server" Text="Delete" CausesValidation="False"

CommandName="Delete"></asp:LinkButton>

</ItemTemplate>

</asp:TemplateField>

</Columns>

<SelectedRowStyle ForeColor="#F7F7F7" Font-Bold="True" BackColor="#738A9C"></SelectedRowStyle>

<RowStyle ForeColor="#4A3C8C" BackColor="#E7E7FF"></RowStyle>

</asp:GridView>

<asp:DetailsView ID="DetailsView1" Runat="server" DataKeyNames="CustomerID" DataSourceID="SqlDataSource3"

BorderWidth="1px" BackColor="LightGoldenrodYellow" GridLines="None" CellPadding="2"

BorderColor="Tan" ForeColor="Black" AutoGenerateRows="False" OnModeChanging="DetailsView1_ModeChanging"

OnItemInserted="DetailsView1_ItemInserted">

<FooterStyle BackColor="Tan"></FooterStyle>

<PagerStyle ForeColor="DarkSlateBlue" HorizontalAlign="Center" BackColor="PaleGoldenrod"></PagerStyle>

<Fields>

<asp:BoundField ReadOnly="True" HeaderText="CustomerID" DataField="CustomerID" SortExpression="CustomerID"></asp:BoundField>

<asp:BoundField HeaderText="CompanyName" DataField="CompanyName" SortExpression="CompanyName"></asp:BoundField>

<asp:BoundField HeaderText="ContactName" DataField="ContactName" SortExpression="ContactName"></asp:BoundField>

<asp:BoundField HeaderText="ContactTitle" DataField="ContactTitle" SortExpression="ContactTitle"></asp:BoundField>

<asp:BoundField HeaderText="Address" DataField="Address" SortExpression="Address"></asp:BoundField>

<asp:BoundField HeaderText="City" DataField="City" SortExpression="City"></asp:BoundField>

<asp:BoundField HeaderText="Region" DataField="Region" SortExpression="Region"></asp:BoundField>

<asp:BoundField HeaderText="PostalCode" DataField="PostalCode" SortExpression="PostalCode"></asp:BoundField>

<asp:BoundField HeaderText="Country" DataField="Country" SortExpression="Country"></asp:BoundField>

<asp:BoundField HeaderText="Phone" DataField="Phone" SortExpression="Phone"></asp:BoundField>

<asp:BoundField HeaderText="Fax" DataField="Fax" SortExpression="Fax"></asp:BoundField>

<asp:CommandField ShowInsertButton="True" ShowEditButton="True"></asp:CommandField>

</Fields>

<HeaderStyle Font-Bold="True" BackColor="Tan"></HeaderStyle>

<EditRowStyle ForeColor="GhostWhite" BackColor="DarkSlateBlue"></EditRowStyle>

<AlternatingRowStyle BackColor="PaleGoldenrod"></AlternatingRowStyle>

</asp:DetailsView>  

<asp:SqlDataSource ID="SqlDataSource1" Runat="server" UpdateCommand="UPDATE [Customers] SET [CompanyName] = @CompanyName, [ContactName] = @ContactName, [Country] = @Country WHERE [CustomerID] = @original_CustomerID"

InsertCommand="INSERT INTO [Customers] ([CustomerID], [CompanyName], [ContactName], [Country]) VALUES (@CustomerID, @CompanyName, @ContactName, @Country)"

DeleteCommand="DELETE FROM [Customers] WHERE [CustomerID] = @original_CustomerID"

SelectCommand="SELECT [CustomerID], [CompanyName], [ContactName], [Country] FROM [Customers] WHERE ([Country] = @Country)"

ConnectionString="<%$ ConnectionStrings:AppConnectionString1 %>">

<DeleteParameters>

<asp:Parameter Type="String" Name="CustomerID"></asp:Parameter>

</DeleteParameters>

<UpdateParameters>

<asp:Parameter Type="String" Name="CompanyName"></asp:Parameter>

<asp:Parameter Type="String" Name="ContactName"></asp:Parameter>

<asp:Parameter Type="String" Name="Country"></asp:Parameter>

<asp:Parameter Type="String" Name="CustomerID"></asp:Parameter>

</UpdateParameters>

<SelectParameters>

<asp:ControlParameter Name="Country" Type="String" ControlID="DropDownList1" PropertyName="SelectedValue"></asp:ControlParameter>

</SelectParameters>

<InsertParameters>

<asp:Parameter Type="String" Name="CustomerID"></asp:Parameter>

<asp:Parameter Type="String" Name="CompanyName"></asp:Parameter>

<asp:Parameter Type="String" Name="ContactName"></asp:Parameter>

<asp:Parameter Type="String" Name="Country"></asp:Parameter>

</InsertParameters>

</asp:SqlDataSource>

<asp:SqlDataSource ID="SqlDataSource2" Runat="server" SelectCommand="SELECT DISTINCT [Country] FROM [Customers]"

ConnectionString="<%$ ConnectionStrings:AppConnectionString1 %>">

</asp:SqlDataSource> <asp:SqlDataSource ID="SqlDataSource3" Runat="server" UpdateCommand="UPDATE [Customers] SET [CompanyName] = @CompanyName, [ContactName] = @ContactName, [ContactTitle] = @ContactTitle, [Address] = @Address, [City] = @City, [Region] = @Region, [PostalCode] = @PostalCode, [Country] = @Country, [Phone] = @Phone, [Fax] = @Fax WHERE [CustomerID] = @original_CustomerID"

InsertCommand="INSERT INTO [Customers] ([CustomerID], [CompanyName], [ContactName], [ContactTitle], [Address], [City], [Region], [PostalCode], [Country], [Phone], [Fax]) VALUES (@CustomerID, @CompanyName, @ContactName, @ContactTitle, @Address, @City, @Region, @PostalCode, @Country, @Phone, @Fax)"

DeleteCommand="DELETE FROM [Customers] WHERE [CustomerID] = @original_CustomerID"

SelectCommand="SELECT * FROM [Customers] WHERE ([CustomerID] = @CustomerID)"

ConnectionString="<%$ ConnectionStrings:AppConnectionString1 %>">

<DeleteParameters>

<asp:Parameter Type="String" Name="CustomerID"></asp:Parameter>

</DeleteParameters>

<UpdateParameters>

<asp:Parameter Type="String" Name="CompanyName"></asp:Parameter>

<asp:Parameter Type="String" Name="ContactName"></asp:Parameter>

<asp:Parameter Type="String" Name="ContactTitle"></asp:Parameter>

<asp:Parameter Type="String" Name="Address"></asp:Parameter>

<asp:Parameter Type="String" Name="City"></asp:Parameter>

<asp:Parameter Type="String" Name="Region"></asp:Parameter>

<asp:Parameter Type="String" Name="PostalCode"></asp:Parameter>

<asp:Parameter Type="String" Name="Country"></asp:Parameter>

<asp:Parameter Type="String" Name="Phone"></asp:Parameter>

<asp:Parameter Type="String" Name="Fax"></asp:Parameter>

<asp:Parameter Type="String" Name="CustomerID"></asp:Parameter>

</UpdateParameters>

<SelectParameters>

<asp:ControlParameter Name="CustomerID" Type="String" ControlID="GridView1" PropertyName="SelectedValue"></asp:ControlParameter>

</SelectParameters>

<InsertParameters>

<asp:Parameter Type="String" Name="CustomerID"></asp:Parameter>

<asp:Parameter Type="String" Name="CompanyName"></asp:Parameter>

<asp:Parameter Type="String" Name="ContactName"></asp:Parameter>

<asp:Parameter Type="String" Name="ContactTitle"></asp:Parameter>

<asp:Parameter Type="String" Name="Address"></asp:Parameter>

<asp:Parameter Type="String" Name="City"></asp:Parameter>

<asp:Parameter Type="String" Name="Region"></asp:Parameter>

<asp:Parameter Type="String" Name="PostalCode"></asp:Parameter>

<asp:Parameter Type="String" Name="Country"></asp:Parameter>

<asp:Parameter Type="String" Name="Phone"></asp:Parameter>

<asp:Parameter Type="String" Name="Fax"></asp:Parameter>

</InsertParameters>

</asp:SqlDataSource>

<asp:Label ID="lblStatus" Runat="server" Text="Label"></asp:Label>

</div>

</form>

</body>

</html>

4)Default.aspx.vb

Partial Class Default_aspx

Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)

GridView1.EditIndex = -1

End Sub

Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

lblStatus.Text = String.Empty

End Sub

Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)

If e.Row.RowType = DataControlRowType.DataRow Then

' Find the Delete LinkButton so we can add

' JavaScript to prompt the user before deleting.

Dim deleteButton As LinkButton = _

CType(e.Row.FindControl("lnkDelete"), LinkButton)

If deleteButton IsNot Nothing Then

deleteButton.Attributes.Add("onClick", _

"return confirm('Are you sure?')")

End If

End If

End Sub

Sub GridView1_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs)

If GridView1.EditIndex <> -1 AndAlso _

GridView1.EditIndex <> e.NewEditIndex Then

lblStatus.Text = "Please finish the current edit" & _

" before moving to a different row."

e.Cancel = True

End If

End Sub

Sub GridView1_SelectedIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSelectEventArgs)

If GridView1.EditIndex <> -1 Then

lblStatus.Text = _

"Please finish the current edit " & _

"before moving to a different row."

e.Cancel = True

End If

End Sub

Sub GridView1_RowDeleted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeletedEventArgs)

If e.Exception IsNot Nothing Then

lblStatus.Text = e.Exception.Message

e.ExceptionHandled = True

End If

End Sub

Sub ShowControls(ByVal blnShow As Boolean)

GridView1.Visible = blnShow

Panel1.Visible = blnShow

End Sub

Sub DetailsView1_ModeChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewModeEventArgs)

ShowControls(e.NewMode <> DetailsViewMode.Insert)

End Sub

Sub btnAddCustomer_Click(ByVal sender As Object, ByVal e As System.EventArgs)

DetailsView1.ChangeMode(DetailsViewMode.Insert)

ShowControls(False)

End Sub

Sub DetailsView1_ItemInserted(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewInsertedEventArgs)

If e.Exception IsNot Nothing Then

lblStatus.Text = e.Exception.Message

e.ExceptionHandled = True

Else

Dim objValue As Object

Dim strCountry As String = String.Empty

Dim strCustID As String = String.Empty

objValue = e.Values("Country")

If objValue IsNot Nothing Then

strCountry = objValue.ToString

End If

objValue = e.Values("CustomerID")

If objValue IsNot Nothing Then

strCustID = objValue.ToString

End If

If strCountry.Length > 0 Then

' If the country does not exist, add it

' to the dropdown list.

Dim item As New ListItem(strCountry)

If Not DropDownList1.Items. _

Contains(item) Then

Me.DropDownList1.Items.Add(item)

End If

' Select the new country.

Me.DropDownList1.SelectedValue = strCountry

End If

ViewState("CustID") = strCustID

End If

End Sub

Private Function GetIndexByCustomerID( _

ByVal CustID As String) As Integer

Dim retval As Integer = -1

For Each row As GridViewRow In GridView1.Rows

If row.RowType = _

DataControlRowType.DataRow Then

If row.Cells(1).Text.Trim = CustID Then

retval = row.RowIndex

Exit For

End If

End If

Next

Return retval

End Function

Sub GridView1_DataBound(ByVal sender As Object, ByVal e As System.EventArgs)

Dim objCustID As Object = ViewState("CustID")

If objCustID IsNot Nothing Then

GridView1.SelectedIndex = _

GetIndexByCustomerID(objCustID.ToString())

End If

' Make sure to clear the ViewState

' value for the next time around.

ViewState("CustID") = Nothing

End Sub

End Class

5)GridView\GridViewCode.VB.txt

' ===================================

' CODE SNIPPET 1

' In Page.Load event

' ===================================

' Make sure the status label is clear each time the page is loaded.

lblStatus.Text = String.Empty

' ===================================

' CODE SNIPPET 2

' In GridView.RowCreated event

' ===================================

If e.Row.RowType = DataControlRowType.DataRow Then

' Find the Delete LinkButton so we can add

' JavaScript to prompt the user before deleting.

Dim deleteButton As LinkButton = _

CType(e.Row.FindControl("lnkDelete"), LinkButton)

If deleteButton IsNot Nothing Then

deleteButton.OnClientClick = _

"return confirm('Are you sure?')"

End If

End If

' ===================================

' ===================================

' CODE SNIPPET 3

' In GridView.RowEditing event

' ===================================

If GridView1.EditIndex <> -1 AndAlso _

GridView1.EditIndex <> e.NewEditIndex Then

lblStatus.Text = "Please finish the current edit" & _

" before moving to a different row."

e.Cancel = True

End If

' ===================================

 

' ===================================

' CODE SNIPPET 4

' In GridView.SelectedIndexChanging

' ===================================

If GridView1.EditIndex <> -1 Then

lblStatus.Text = _

"Please finish the current edit " & _

"before moving to a different row."

e.Cancel = True

End If

' ===================================

' ===================================

' CODE SNIPPET 5

' In GridView.RowDeleted

' ===================================

If e.Exception IsNot Nothing Then

lblStatus.Text = e.Exception.Message

e.ExceptionHandled = True

Else

GridView1.SelectedIndex = -1

End If

' ===================================

' ===================================

' CODE SNIPPET 6

' Replace Gridview.SelectedIndexChanging

' ===================================

If (GridView1.EditIndex <> -1) Or _

(DetailsView1.CurrentMode = DetailsViewMode.Edit) Then

lblStatus.Text = _

"Please finish the current edit " & _

"before moving to a different row."

e.Cancel = True

End If

' ===================================

' ===================================

' CODE SNIPPET 7

' ===================================

Sub ShowControls(ByVal blnShow As Boolean)

GridView1.Visible = blnShow

Panel1.Visible = blnShow

End Sub

' ===================================

' ===================================

' CODE SNIPPET 8

' In DetailsView.ModeChanging

' ===================================

ShowControls(e.NewMode <> DetailsViewMode.Insert)

' ===================================

' ===================================

' CODE SNIPPET 9

' In btnAddCustomer.Click

' ===================================

DetailsView1.ChangeMode(DetailsViewMode.Insert)

ShowControls(False)

' ===================================

' ===================================

' CODE SNIPPET 10

' In DetailsView.ItemInserted event handler

' ===================================

If e.Exception IsNot Nothing Then

lblStatus.Text = e.Exception.Message

e.ExceptionHandled = True

Else

End If

' ===================================

' ===================================

' CODE SNIPPET 11

' In DetailsView.ItemInserted event handler

' Inside the ELSE statement

' ===================================

Dim objValue As Object

Dim strCountry As String = String.Empty

Dim strCustID As String = String.Empty

objValue = e.Values("Country")

If objValue IsNot Nothing Then

strCountry = objValue.ToString

End If

objValue = e.Values("CustomerID")

If objValue IsNot Nothing Then

strCustID = objValue.ToString

End If

' ===================================

' ===================================

' CODE SNIPPET 12

' In the ELSE statement:

' ===================================

If strCountry.Length > 0 Then

' If the country does not exist, add it

' to the dropdown list.

Dim item As New ListItem(strCountry)

If Not DropDownList1.Items. _

Contains(item) Then

Me.DropDownList1.Items.Add(item)

End If

' Select the new country.

Me.DropDownList1.SelectedValue = strCountry

End If

' ===================================

' ===================================

' CODE SNIPPET 13

' ===================================

ViewState("CustID") = strCustID

' ===================================

' ===================================

' CODE SNIPPET 14

' ===================================

Private Function GetIndexByCustomerID( _

ByVal CustID As String) As Integer

Dim retval As Integer = -1

For Each row As GridViewRow In GridView1.Rows

If row.RowType = _

DataControlRowType.DataRow Then

If row.Cells(1).Text.Trim = CustID Then

retval = row.RowIndex

Exit For

End If

End If

Next

Return retval

End Function

' ===================================

' ===================================

' CODE SNIPPET 15

' Check to see if the CustomerID has been stored in ViewState.

' If so, this means that we're finishing up a new customer

' added in the ItemInserted event of the DetailsView control. In this

' case, we need to select the new row within the GridView. We know

' that the correct subset of rows will be selected, because we

' set the SelectedValue property of the DropDownList control which filtered

' the grid. If there are multiple pages, however,

' this may not find the correct row, and will end up with

' no row selected.

' ===================================

Dim objCustID As Object = ViewState("CustID")

If objCustID IsNot Nothing Then

GridView1.SelectedIndex = _

GetIndexByCustomerID(objCustID.ToString())

End If

' Make sure to clear the ViewState

' value for the next time around.

ViewState("CustID") = Nothing

' ===================================

5)

' ===================================

' CODE SNIPPET 1

' In Page.Load event

' ===================================

' Make sure the status label is clear each time the page is loaded.

lblStatus.Text = String.Empty

' ===================================

' CODE SNIPPET 2

' In GridView.RowCreated event

' ===================================

If e.Row.RowType = DataControlRowType.DataRow Then

' Find the Delete LinkButton so we can add

' JavaScript to prompt the user before deleting.

Dim deleteButton As LinkButton = _

CType(e.Row.FindControl("lnkDelete"), LinkButton)

If deleteButton IsNot Nothing Then

deleteButton.OnClientClick = _

"return confirm('Are you sure?')"

End If

End If

' ===================================

' ===================================

' CODE SNIPPET 3

' In GridView.RowEditing event

' ===================================

If GridView1.EditIndex <> -1 AndAlso _

GridView1.EditIndex <> e.NewEditIndex Then

lblStatus.Text = "Please finish the current edit" & _

" before moving to a different row."

e.Cancel = True

End If

' ===================================

 

' ===================================

' CODE SNIPPET 4

' In GridView.SelectedIndexChanging

' ===================================

If GridView1.EditIndex <> -1 Then

lblStatus.Text = _

"Please finish the current edit " & _

"before moving to a different row."

e.Cancel = True

End If

' ===================================

' ===================================

' CODE SNIPPET 5

' In GridView.RowDeleted

' ===================================

If e.Exception IsNot Nothing Then

lblStatus.Text = e.Exception.Message

e.ExceptionHandled = True

Else

GridView1.SelectedIndex = -1

End If

' ===================================

' ===================================

' CODE SNIPPET 6

' Replace Gridview.SelectedIndexChanging

' ===================================

If (GridView1.EditIndex <> -1) Or _

(DetailsView1.CurrentMode = DetailsViewMode.Edit) Then

lblStatus.Text = _

"Please finish the current edit " & _

"before moving to a different row."

e.Cancel = True

End If

' ===================================

' ===================================

' CODE SNIPPET 7

' ===================================

Sub ShowControls(ByVal blnShow As Boolean)

GridView1.Visible = blnShow

Panel1.Visible = blnShow

End Sub

' ===================================

' ===================================

' CODE SNIPPET 8

' In DetailsView.ModeChanging

' ===================================

ShowControls(e.NewMode <> DetailsViewMode.Insert)

' ===================================

' ===================================

' CODE SNIPPET 9

' In btnAddCustomer.Click

' ===================================

DetailsView1.ChangeMode(DetailsViewMode.Insert)

ShowControls(False)

' ===================================

' ===================================

' CODE SNIPPET 10

' In DetailsView.ItemInserted event handler

' ===================================

If e.Exception IsNot Nothing Then

lblStatus.Text = e.Exception.Message

e.ExceptionHandled = True

Else

End If

' ===================================

' ===================================

' CODE SNIPPET 11

' In DetailsView.ItemInserted event handler

' Inside the ELSE statement

' ===================================

Dim objValue As Object

Dim strCountry As String = String.Empty

Dim strCustID As String = String.Empty

objValue = e.Values("Country")

If objValue IsNot Nothing Then

strCountry = objValue.ToString

End If

objValue = e.Values("CustomerID")

If objValue IsNot Nothing Then

strCustID = objValue.ToString

End If

' ===================================

' ===================================

' CODE SNIPPET 12

' In the ELSE statement:

' ===================================

If strCountry.Length > 0 Then

' If the country does not exist, add it

' to the dropdown list.

Dim item As New ListItem(strCountry)

If Not DropDownList1.Items. _

Contains(item) Then

Me.DropDownList1.Items.Add(item)

End If

' Select the new country.

Me.DropDownList1.SelectedValue = strCountry

End If

' ===================================

' ===================================

' CODE SNIPPET 13

' ===================================

ViewState("CustID") = strCustID

' ===================================

' ===================================

' CODE SNIPPET 14

' ===================================

Private Function GetIndexByCustomerID( _

ByVal CustID As String) As Integer

Dim retval As Integer = -1

For Each row As GridViewRow In GridView1.Rows

If row.RowType = _

DataControlRowType.DataRow Then

If row.Cells(1).Text.Trim = CustID Then

retval = row.RowIndex

Exit For

End If

End If

Next

Return retval

End Function

' ===================================

' ===================================

' CODE SNIPPET 15

' Check to see if the CustomerID has been stored in ViewState.

' If so, this means that we're finishing up a new customer

' added in the ItemInserted event of the DetailsView control. In this

' case, we need to select the new row within the GridView. We know

' that the correct subset of rows will be selected, because we

' set the SelectedValue property of the DropDownList control which filtered

' the grid. If there are multiple pages, however,

' this may not find the correct row, and will end up with

' no row selected.

' ===================================

Dim objCustID As Object = ViewState("CustID")

If objCustID IsNot Nothing Then

GridView1.SelectedIndex = _

GetIndexByCustomerID(objCustID.ToString())

End If

' Make sure to clear the ViewState

' value for the next time around.

ViewState("CustID") = Nothing

' ===================================

6)GridView\GridViewVB.sln

Microsoft Visual Studio Solution File, Format Version 9.00

# Visual Studio 2005

Project("{E24C65DC-7377-472B-9ABA-BC803B73C61A}") = "C:\ASPNET20VB\GridView\", "C:\ASPNET20VB\GridView\", "{FA22191B-4C95-4059-A1B7-FFE2B58711F4}"

ProjectSection(WebsiteProperties) = preProject

VWDPort = 7684

EndProjectSection

EndProject

Global

GlobalSection(SolutionConfigurationPlatforms) = preSolution

Debug|.NET = Debug|.NET

EndGlobalSection

GlobalSection(ProjectConfigurationPlatforms) = postSolution

{FA22191B-4C95-4059-A1B7-FFE2B58711F4}.Debug|.NET.ActiveCfg = Debug|.NET

{FA22191B-4C95-4059-A1B7-FFE2B58711F4}.Debug|.NET.Build.0 = Debug|.NET

EndGlobalSection

GlobalSection(SolutionProperties) = preSolution

HideSolutionNode = FALSE

EndGlobalSection

EndGlobal

7)GridView\Web.config

<?xml version="1.0"?>

<!-- Note: As an alternative to hand editing this file you can use the web admin tool to

configure settings for your application. Use the Website->Asp.Net Configuration option

in Visual Studio.

A full list of settings and comments can be found in machine.config.comments usually

located in \Windows\Microsft.Net\Frameworks\v2.x\Config -->

<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">

<appSettings/>

<connectionStrings>

<add name="AppConnectionString1" connectionString="Server=(local);Integrated Security=True;Database=Northwind" providerName="System.Data.SqlClient"/>

</connectionStrings>

<system.web>

<!--

Set compilation debug="true" to insert debugging symbols into the compiled page.

Because this affects performance, set this value to true only during development.

-->

<compilation debug="true"/>

<!--

The <authentication> section enables configuration of the security authentication

mode used by ASP.NET to identify an incoming user.

-->

<authentication mode="Windows"/>

<!--

The <customErrors> section enables configuration of what to do if/when an unhandled

error occurs during the execution of a request. Specifically, it enables developers

to configure html error pages to be displayed in place of a error stack trace.

-->

<customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">

<!--

<error statusCode="403" redirect="NoAccess.htm"/>

<error statusCode="404" redirect="FileNotFound.htm"/>

-->

</customErrors>

</system.web>

</configuration>


 

 
Fans of "The Office"
Dwight Bobbleheads are here!
  “It's me! I'm the bobblehead! Yes!”



Advertise on XMLPitstop

Advertise on XMLPitstop


EggHead Cafe
Web Servicee development
DotNetSlackers
Buy text ads here!
laptop online backup
Color Laser Printer
Bvlgari sunglasses
Skype vs. sipcall
VoIP Internettelefonie
Buy text ads here!

Interested in Text ads?
2,223 Total Members
36 members(last 30 days)
8 members(last 7 days)
2 members(today)

1,609 Total Discussions
12 Posts(last 30 days)
0 Posts(last 7 days)
0 Posts(today)

17,260 Total Blog Posts
1,839 Blogs(last 30 days)
342 Blogs(last 7 days)
49 Blogs(today)

8,699 Newsgroup Posts
0 Posts(last 30 days)
0 Posts(last 7 days)
0 Posts(today)

13,786 Total Resources
5 Resources(last 30 days)
1 Resources(last 7 days)
0 Resources(today)


 

David Silverlight's XMLPitstop.com| 2801 Florida Ave #225|Miami, FL 33133|Ph:305-447-1139