codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:
Search Forums:
  Datagrid formating and postback  niloc at 16:09 on Thursday, September 22, 2005
 

Hi

Hope someone can help me with this...

I have a datagrid to which I am binding a datatable.

The Table has Subheadings and I am using the itemDataBound event to format these headings.

This works fine until a postback.

The itemDataBound event is not fired so the formatting is lost.

I have added a workaround that rebinds the table. but this would be prohibitive on large datasets.

Any suggestions, alternative methods welcome.

I have pasted a sample page below for you to try, so you will see what i mean.

Thanks

Col

TestGrid.aspx.vb

Public Class TestGrid
Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

End Sub
Protected WithEvents lblSelected As System.Web.UI.WebControls.Label
Protected WithEvents btnDelete As System.Web.UI.WebControls.Button
Protected WithEvents dgUsers As System.Web.UI.WebControls.DataGrid

'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Dim ds As New DataSet

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
If Not Page.IsPostBack Then 'initial load
createTable()
bindGrid()
End If
End Sub

Private Sub createTable()
ds.Tables.Add("Users")
ds.Tables("Users").Columns.Add("Name")
ds.Tables("Users").Columns.Add("Age")
ds.Tables("Users").Columns.Add("Address")
ds.Tables("Users").Columns.Add("ID")
End Sub

Private Sub bindGrid()
Dim row As DataRow
row = ds.Tables("Users").NewRow

row.Item("Name") = "BOYS"
row.Item("Age") = ""
row.Item("Address") = ""
row.Item("ID") = ""
ds.Tables("Users").Rows.Add(row)

row = ds.Tables("Users").NewRow
row.Item("Name") = "Tom"
row.Item("Age") = "25"
row.Item("Address") = "Manchester"
row.Item("ID") = 1
ds.Tables("Users").Rows.Add(row)

row = ds.Tables("Users").NewRow
row.Item("Name") = "Dick"
row.Item("Age") = "31"
row.Item("Address") = "Bristol"
row.Item("ID") = 2
ds.Tables("Users").Rows.Add(row)

row = ds.Tables("Users").NewRow
row.Item("Name") = "Harry"
row.Item("Age") = "45"
row.Item("Address") = "London"
row.Item("ID") = 3
ds.Tables("Users").Rows.Add(row)

row = ds.Tables("Users").NewRow
row.Item("Name") = "GIRLS"
row.Item("Age") = ""
row.Item("Address") = ""
row.Item("ID") = ""
ds.Tables("Users").Rows.Add(row)

row = ds.Tables("Users").NewRow
row.Item("Name") = "Gertrude"
row.Item("Age") = "21"
row.Item("Address") = "Liverpool"
row.Item("ID") = 4
ds.Tables("Users").Rows.Add(row)

row = ds.Tables("Users").NewRow
row.Item("Name") = "Maybel"
row.Item("Age") = "40"
row.Item("Address") = "Plymouth"
row.Item("ID") = 5
ds.Tables("Users").Rows.Add(row)

row = ds.Tables("Users").NewRow
row.Item("Name") = "Sue"
row.Item("Age") = "56"
row.Item("Address") = "Leeds"
row.Item("ID") = 6
ds.Tables("Users").Rows.Add(row)

dgUsers.DataSource = ds.Tables("Users")
dgUsers.DataBind()

End Sub

Sub dgSearch_ItemDataBound(ByVal sender As Object, ByVal e As DataGridItemEventArgs)
Select Case e.Item.ItemType
Case ListItemType.AlternatingItem, ListItemType.Item 'Check the Row Type
'if we have a Subheader!
If e.Item.Cells(0).Text.Equals("BOYS") Or e.Item.Cells(0).Text.Equals("GIRLS") Then

Dim NoOfCols As Integer = e.Item.Cells.Count - 1
Dim StartCol As Integer = 1
e.Item.Cells(0).ColumnSpan = NoOfCols + 1

'Remove all the cells except the sub header
Dim i As Integer
For i = NoOfCols To StartCol Step -1
e.Item.Cells.RemoveAt(i)
Next

e.Item.Cells(0).BackColor = Color.FromArgb(204, 204, 255)
End If
End Select

End Sub


Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
Dim dgItem As DataGridItem
Dim chkSelected As CheckBox
Dim strUserID As Integer

For Each dgItem In dgUsers.Items
chkSelected = dgItem.FindControl("chkSelection")
If chkSelected.Checked Then
strUserID = CType(dgItem.FindControl("lblID"), Label).Text
lblSelected.Text += "Plant ID: <b>" & strUserID & "</b>, "
End If
Next

' this is a work around
'createTable()
'bindGrid()

End Sub
End Class

TestGrid.aspx

<%@ Page Language="vb" AutoEventWireup="false" Codebehind="TestGrid.aspx.vb" Inherits="PPTest.TestGrid"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>TestGrid</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:button id="btnDelete" style="Z-INDEX: 123; LEFT: 8px; POSITION: absolute; TOP: 8px" runat="server"
Text="Delete"></asp:button><asp:label id="lblSelected" style="Z-INDEX: 122; LEFT: 72px; POSITION: absolute; TOP: 16px"
runat="server" Height="16px" Width="560px"></asp:label><asp:datagrid id="dgUsers" style="Z-INDEX: 101; LEFT: 8px; POSITION: absolute; TOP: 40px" runat="server"
Height="64px" Width="433px" OnItemDataBound="dgSearch_ItemDataBound" AutoGenerateColumns="False" BackColor="White">
<Columns>
<asp:BoundColumn DataField="Name" HeaderText="Name"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="Delete">
<ItemStyle HorizontalAlign="Center"></ItemStyle>
<ItemTemplate>
<asp:CheckBox ID="chkSelection" Runat="server" />
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="Age" HeaderText="Age"></asp:BoundColumn>
<asp:BoundColumn DataField="Address" HeaderText="Address"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="ID">
<ItemTemplate>
<asp:Label id="lblID" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.ID") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid></form>
</body>
</HTML>












CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums








Recent Forum Threads
•  Why Use Method?
•  Re: Help with filesystem object & displaying in a table
•  Re: Genetic Algorithm Help
•  Re: How to make an investment calculator
•  Re: line breaks in GUI
•  Re: Graph in Gui...
•  Graph in Gui...
•  Re: Counting zero values in a string
•  Re: Help!


Recent Articles
Multiple submit buttons with form validation
Understanding Hibernate ORM for Java/J2EE
HTTP screen-scraping and caching
a javascript calculator
A simple way to JTable
Java Native Interface (JNI)
Parsing Dynamic Layouts
MagicGrid
Caching With ASP.Net
Creating CSS Buttons


Site Survey
Help us serve you better. Take a five minute survey. Click here!

© Copyright codetoad.com 2001-2005