The APS.Net
DataGrid
Sample
at: ../Datagrid/AspDataGrid.aspx
E.Item.DataItem Sample…
If e.Item.ItemType =
ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem
Then
Dim o As Label
o = e.Item.FindControl("lblExam")
o.Text = e.Item.DataItem("Exam")
End If
Private
Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid1.ItemDataBound
If
e.Item.ItemType = ListItemType.Item Or
e.Item.ItemType = ListItemType.AlternatingItem Then
Dim o As Label
o =
e.Item.Cells(3).FindControl("lblLink")
o.Text = "<a id='MyId'
href=""javascript:PopIt('Some Text To
Show...<br><br>Hello World!');""'style='background-color:LightBlue;
border-color:CornflowerBlue; border-width:2px; border-style:Solid; height:18px;
padding:3px; text-align:center'>Click Here</a>"
End If
End Sub
How to format a date field in a cell of a DataGrid…
If
e.Item.ItemType = ListItemType.AlternatingItem Or e.Item.ItemType = ListItemType.Item Then
e.Item.Cells(4).Text = d.ToShortDateString
'Do Nothing If
there is not date in the database field
'Here's how to
reference a TextBox found in an Item Template....
If e.Item.ItemType = ListItemType.Item Or
e.Item.ItemType = ListItemType.AlternatingItem Then
CType(e.Item.FindControl("TextBox1"),
TextBox).Text = "Hello Worldie!"
'Here's how to
respond to a ButtonColumn Click Event....
If
e.CommandName.ToUpper = "CMDMYBUTTON" Then
Response.Write(CType(e.Item.FindControl("TextBox1"), TextBox).Text)
Just
adding the code below to the DataGrid’s EditCommand Event causes the grid to look like
TextBoxes now in the cells and the Update,
Cancel and Delete Buttons Appear....
DataGrid1.SelectedIndex = e.Item.ItemIndex
DataGrid1.EditItemIndex = e.Item.ItemIndex
Responding
to a Delete Command...
Private Sub
DataGrid1_DeleteCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs) Handles
DataGrid1.DeleteCommand
DataGrid1.SelectedIndex =
e.Item.ItemIndex
Response.Write("Delete This Item?
" & e.Item.Cells(0).Text())
Canceling
the edited data is as easy as placing this code in the DataGrid’s CancelCommand
event…
Reference a
cell not in edit mode:
If
e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
CType(e.Item.FindControl("TextBox1"),
TextBox).Text = Val(e.Item.Cells(1).Text) * 10
'*******************
OPEN A DATABASE ****************************************************
Dim objCommand As New
OleDbCommand()
Dim objDa As New OleDbDataAdapter()
Dim
objCommandBuilder As New
OleDbCommandBuilder()
Dim strSQL As String =
"Select Lname From candidate Where lname like 'z%'"
objCommand.CommandText = strSQL
objCommand.Connection = objConn
objDa.SelectCommand = objCommand
'Show All The Data
In the DataSet....
DataGrid1.DataSource = objDs.Tables("CanDem")
MessageBox.Show("Now We'll Filter The Data")
'Now Filter The
Data Shown In The Grid...
objDs.Tables("CanDem").DefaultView.RowFilter = "Lname
like 'ze%'"
DataGrid1.DataSource = objDs.Tables("CanDem")
Reference
a cell in edit mode:
strName
= CType(e.Item.Cells(0).Controls(0), TextBox).Text
Private Sub DataGrid1_ItemCreated(xxx)
Handles DataGrid1.ItemCreated
e.Item.Cells(1).Width = New Unit(10)
If e.CommandName
= "MyTemplateButton" Then
DataGrid1.SelectedIndex = e.Item.ItemIndex
Response.Write(CType(DataGrid1.SelectedItem.FindControl("txtTI"),
TextBox).Text)
'Add a javascript onclick event at
runtime....
If e.Item.ItemType = ListItemType.Item Or
e.Item.ItemType = ListItemType.AlternatingItem Then
B =
CType(e.Item.FindControl("cmdDelete"), Button)
B.Attributes.Add("onclick",
"return confirm('Are you sure you want to delete this record?');")
If
e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
B =
e.Item.Cells(6).Controls(0)
B.Attributes.Add("onclick",
"return confirm('Are you sure you want to delete this record?');")
If
e.Item.ItemType = ListItemType.AlternatingItem Then
CType(e.Item.FindControl("txtCheckAmount"), TextBox).BackColor
= System.Drawing.Color.Salmon
CType(e.Item.FindControl("txtCheckDate"),
TextBox).BackColor = System.Drawing.Color.Salmon
CType(e.Item.FindControl("txtService"), TextBox).BackColor =
System.Drawing.Color.Salmon
Use
The DataView to filter records…
dv = New
DataView(objDataSet.Tables("Proctors"))
dv.RowFilter = "Lname = 'Schimsky'
and fname = 'Steve'"
CreateTableBindData() ‘then all
we need to do is sort the data and re-bind.