B
Burak Gunay
Hello everyone,
I am including the code and I am going to describe the issue I am
having. I would appreciate it if you would take a look at this.
Ok, what I am trying to do is to have a dynamic datagrid where we are
pulling the column names and the column data from a dataset. 1st step
is to define a textboxcolumn class as follows
Class myTextBoxColumn
Implements ITemplate
Dim templateType As ListItemType
Dim columnName As String
Sub New(ByVal type As ListItemType, ByVal ColName As String)
templateType = type
columnName = ColName
End Sub
Public Sub InstantiateIn(ByVal container As
System.Web.UI.Control) Implements System.Web.UI.ITemplate.InstantiateIn
Select Case templateType
Case ListItemType.Item
Dim lc As New Literal
AddHandler lc.DataBinding, AddressOf
Me.BindLiteralColumn
container.Controls.Add(lc)
Case ListItemType.EditItem
Dim tb As New TextBox
If columnName = "id" Then
tb.Enabled = False
End If
If columnName <> "Reason" Then
AddHandler tb.DataBinding, AddressOf
Me.BindTextBoxColumn
Else
tb.ID = "txtReason"
tb.TextMode = TextBoxMode.MultiLine
tb.Rows = "4"
tb.Width =
System.Web.UI.WebControls.Unit.Pixel(185)
tb.MaxLength = 200
End If
container.Controls.Add(tb)
End Select
End Sub
Sub BindTextBoxColumn(ByVal sender As Object, ByVal e As
EventArgs)
Dim txtBox As TextBox = CType(sender, TextBox)
Dim container As DataGridItem =
CType(txtBox.NamingContainer, DataGridItem)
txtBox.ID = columnName
txtBox.Text =
Convert.ToString(DataBinder.Eval((CType(container,
DataGridItem)).DataItem, columnName))
End Sub
Sub BindLiteralColumn(ByVal sender As Object, ByVal e As
EventArgs)
Dim lc As Literal = CType(sender, Literal)
Dim container As DataGridItem = CType(lc.NamingContainer,
DataGridItem)
lc.ID = columnName
lc.Text =
Convert.ToString(DataBinder.Eval((CType(container,
DataGridItem)).DataItem, columnName))
End Sub
End Class
and then we pull data from a table and loop through it to fill the
datagrid "dtgValues"
Sub FillGrid()
' get dataset with record values
Dim ds As DataSet = GetRecords(sql)
Dim i, count As Integer
Dim colName As String
' go through the list of columns
count = ds.Tables(0).Columns.Count
If count > 0 Then
For i = 0 To count - 1
colName = ds.Tables(0).Columns(i).ColumnName
Dim col As New TemplateColumn
col.ItemTemplate = New myTextBoxColumn(ListItemType.Item, colName)
col.EditItemTemplate = New myTextBoxColumn(ListItemType.EditItem,
colName)
col.HeaderText = colName
col.HeaderStyle.ForeColor = System.Drawing.Color.White
dtgValues.Columns.Add(col)
Next
' add edit column
Dim colEdit As New EditCommandColumn
colEdit.ButtonType = ButtonColumnType.PushButton
colEdit.UpdateText = "Update"
colEdit.EditText = "Edit"
colEdit.CancelText = "Cancel"
dtgValues.Columns.Add(colEdit)
' bind data
dtgValues.DataSource = ds.Tables(0)
dtgValues.DataBind()
End If
End Sub
now all this works fine, the columns and the values show up. Since this
datagrid gets created dynamically, we need to create the datagrid on
each pageload as shown, so as not to lose the values in the datagrid
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
FillGrid()
End Sub
Ok, the problem is, when the Edit button is clicked, nothing happens,
and if you click it a second time, it automatically goes into Update
mode.
Private Sub dtgValues_EditCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs) Handles
dtgValues.EditCommand
' Set the EditItemIndex property to the index of the item clicked
' in the DataGrid control to enable editing for that item.
dtgValues.EditItemIndex = e.Item.ItemIndex
Session("editIndex") = e.Item.ItemIndex
' FillGrid(e.Item.ItemIndex)
End Sub
How can we have the edit button work just like a datagrid with all the
columns predefined?.
Thank you,
Burak
I am including the code and I am going to describe the issue I am
having. I would appreciate it if you would take a look at this.
Ok, what I am trying to do is to have a dynamic datagrid where we are
pulling the column names and the column data from a dataset. 1st step
is to define a textboxcolumn class as follows
Class myTextBoxColumn
Implements ITemplate
Dim templateType As ListItemType
Dim columnName As String
Sub New(ByVal type As ListItemType, ByVal ColName As String)
templateType = type
columnName = ColName
End Sub
Public Sub InstantiateIn(ByVal container As
System.Web.UI.Control) Implements System.Web.UI.ITemplate.InstantiateIn
Select Case templateType
Case ListItemType.Item
Dim lc As New Literal
AddHandler lc.DataBinding, AddressOf
Me.BindLiteralColumn
container.Controls.Add(lc)
Case ListItemType.EditItem
Dim tb As New TextBox
If columnName = "id" Then
tb.Enabled = False
End If
If columnName <> "Reason" Then
AddHandler tb.DataBinding, AddressOf
Me.BindTextBoxColumn
Else
tb.ID = "txtReason"
tb.TextMode = TextBoxMode.MultiLine
tb.Rows = "4"
tb.Width =
System.Web.UI.WebControls.Unit.Pixel(185)
tb.MaxLength = 200
End If
container.Controls.Add(tb)
End Select
End Sub
Sub BindTextBoxColumn(ByVal sender As Object, ByVal e As
EventArgs)
Dim txtBox As TextBox = CType(sender, TextBox)
Dim container As DataGridItem =
CType(txtBox.NamingContainer, DataGridItem)
txtBox.ID = columnName
txtBox.Text =
Convert.ToString(DataBinder.Eval((CType(container,
DataGridItem)).DataItem, columnName))
End Sub
Sub BindLiteralColumn(ByVal sender As Object, ByVal e As
EventArgs)
Dim lc As Literal = CType(sender, Literal)
Dim container As DataGridItem = CType(lc.NamingContainer,
DataGridItem)
lc.ID = columnName
lc.Text =
Convert.ToString(DataBinder.Eval((CType(container,
DataGridItem)).DataItem, columnName))
End Sub
End Class
and then we pull data from a table and loop through it to fill the
datagrid "dtgValues"
Sub FillGrid()
' get dataset with record values
Dim ds As DataSet = GetRecords(sql)
Dim i, count As Integer
Dim colName As String
' go through the list of columns
count = ds.Tables(0).Columns.Count
If count > 0 Then
For i = 0 To count - 1
colName = ds.Tables(0).Columns(i).ColumnName
Dim col As New TemplateColumn
col.ItemTemplate = New myTextBoxColumn(ListItemType.Item, colName)
col.EditItemTemplate = New myTextBoxColumn(ListItemType.EditItem,
colName)
col.HeaderText = colName
col.HeaderStyle.ForeColor = System.Drawing.Color.White
dtgValues.Columns.Add(col)
Next
' add edit column
Dim colEdit As New EditCommandColumn
colEdit.ButtonType = ButtonColumnType.PushButton
colEdit.UpdateText = "Update"
colEdit.EditText = "Edit"
colEdit.CancelText = "Cancel"
dtgValues.Columns.Add(colEdit)
' bind data
dtgValues.DataSource = ds.Tables(0)
dtgValues.DataBind()
End If
End Sub
now all this works fine, the columns and the values show up. Since this
datagrid gets created dynamically, we need to create the datagrid on
each pageload as shown, so as not to lose the values in the datagrid
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
FillGrid()
End Sub
Ok, the problem is, when the Edit button is clicked, nothing happens,
and if you click it a second time, it automatically goes into Update
mode.
Private Sub dtgValues_EditCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs) Handles
dtgValues.EditCommand
' Set the EditItemIndex property to the index of the item clicked
' in the DataGrid control to enable editing for that item.
dtgValues.EditItemIndex = e.Item.ItemIndex
Session("editIndex") = e.Item.ItemIndex
' FillGrid(e.Item.ItemIndex)
End Sub
How can we have the edit button work just like a datagrid with all the
columns predefined?.
Thank you,
Burak