Microsoft
Software
Hardware
Network
Question : Add new row to datagrid for insertion vb.net
Hello Experts,
When i'm using the following code a new row is being inserted to grid but it is replacing the first data row in the grid and makes it in edit mode. can you please chk where i'm missing the steps if any. the grid is working otherwise in edit mode. Please check at btnadd_click and new row isert as well as grid ItemCommand. This is urgent please.
Public Class sample
' Inherits System.Web.UI.Page
Inherits inlibrary.BasePage
Protected WithEvents idSiteMenu As inlibrary.SiteMenuControl
Protected WithEvents idCurrentMenu As CurrentMenu
Protected WithEvents idErrorTable As HtmlTableRow
Protected WithEvents idErrorText As Label
Protected WithEvents btnAdd As Button
Protected WithEvents idMessageTable As HtmlTableRow
Protected WithEvents idMessageText As Label
Protected WithEvents idInfoGrid As DataGrid
Protected idSiteHeader As SiteHeader
Private _dt As DataTable
Private _mode As Modes = Modes.Standard
Public Enum Modes
Standard
ConfirmAdd
End Enum
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
erStepThro
ugh()> Private Sub InitializeComponent()
End Sub
'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclara
tion 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()
SecurityEnabled = False
End Sub
#End Region
Private Sub idInfoGrid_ItemCommand(ByV
al source As Object, ByVal e As System.Web.UI.WebControls.
DataGridCo
mmandEvent
Args) Handles idInfoGrid.ItemCommand
Dim cn As SqlClient.SqlConnection
Dim cmd As SqlClient.SqlCommand
Dim st As SqlClient.SqlTransaction
Dim temp_date As System.DateTime
Dim strMessage As String, strFlg As String, strStatus As String
Dim result As Integer
idInfoGrid.EditItemIndex = -1
Select Case e.CommandName
Case "Insert"
'Insert code here
'idInfoGrid.DataBind()
Case "Edit"
idInfoGrid.EditItemIndex = e.Item.ItemIndex
idInfoGrid.Columns(6).Visi
ble = False
idInfoGrid.Columns(7).Visi
ble = False
idInfoGrid.DataBind()
Case "Cancel"
'idInfoGrid.EditItemIndex = -1
idInfoGrid.Columns(6).Visi
ble = True
idInfoGrid.Columns(7).Visi
ble = True
Dim ecc As WebLibrary.EditCommandColu
mn = DirectCast(idInfoGrid.Colu
mns(0), WebLibrary.EditCommandColu
mn)
ecc.UpdateText = "Update"
idInfoGrid.DataBind()
Case "Update"
' Validate the entered info, and log which entries were changed
idInfoGrid.EditItemIndex = -1
DataBind()
idInfoGrid.Columns(6).Visi
ble = True
idInfoGrid.Columns(7).Visi
ble = True
End Select
End Sub
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' idSiteHeader.Title = idSiteMenu.MenuDescription
If Not IsPostBack Then
idMessageTable.Visible = False
idErrorTable.Visible = False
End If
DataBind()
End Sub
Public Overrides Sub DataBind()
SetUpGrid()
FillGrid()
End Sub
Private Sub SetUpGrid()
' Create the columns for the datagrid if they don't already exist.
Dim ec As WebLibrary.EditCommandColu
mn
Dim bc As BoundColumn
Dim lb As WebLibrary.LinkButtonColum
n
If idInfoGrid.Columns.Count = 0 Then
ec = New WebLibrary.EditCommandColu
mn
ec.HeaderText = " Function "
ec.HeaderStyle.HorizontalA
lign = HorizontalAlign.Center
ec.HeaderStyle.Font.Bold = True
ec.HeaderStyle.BackColor = Color.Blue
ec.ButtonType = ButtonColumnType.LinkButto
n
ec.ItemStyle.CssClass = "submenu"
ec.EditText = "Edit"
ec.CancelText = "Cancel"
ec.UpdateText = "Update"
idInfoGrid.Columns.Add(ec)
bc = New BoundColumn
bc.HeaderText = " Account "
bc.HeaderStyle.HorizontalA
lign = HorizontalAlign.Center
bc.HeaderStyle.Font.Bold = True
bc.HeaderStyle.BackColor = Color.Blue
'bc.ItemStyle.CssClass = "submenu"
bc.ItemStyle.HorizontalAli
gn = HorizontalAlign.Center
'bc.ItemStyle.Width = New Unit("100px")
bc.DataField = "Account"
idInfoGrid.Columns.Add(bc)
bc = New BoundColumn
bc.HeaderText = " Account Name "
bc.HeaderStyle.HorizontalA
lign = HorizontalAlign.Center
bc.HeaderStyle.Font.Bold = True
bc.HeaderStyle.BackColor = Color.Blue
'bc.ItemStyle.CssClass = "submenu"
bc.ItemStyle.HorizontalAli
gn = HorizontalAlign.Center
'bc.ItemStyle.Width = New Unit("100px")
bc.ReadOnly = True
bc.DataField = "AccountName"
idInfoGrid.Columns.Add(bc)
bc = New BoundColumn
bc.HeaderText = " Billed "
bc.HeaderStyle.HorizontalA
lign = HorizontalAlign.Center
bc.HeaderStyle.Font.Bold = True
bc.HeaderStyle.BackColor = Color.Blue
'bc.ItemStyle.CssClass = "submenu"
bc.ItemStyle.HorizontalAli
gn = HorizontalAlign.Center
bc.DataField = "DisplayStatus"
idInfoGrid.Columns.Add(bc)
bc = New BoundColumn
bc.HeaderText = " Label "
bc.HeaderStyle.HorizontalA
lign = HorizontalAlign.Center
bc.HeaderStyle.Font.Bold = True
bc.HeaderStyle.BackColor = Color.Blue
'bc.ItemStyle.CssClass = "submenu"
bc.ItemStyle.HorizontalAli
gn = HorizontalAlign.Center
bc.DataField = "DisplayActiveFlag"
idInfoGrid.Columns.Add(bc)
bc = New BoundColumn
bc.HeaderText = " Price "
bc.HeaderStyle.HorizontalA
lign = HorizontalAlign.Center
bc.HeaderStyle.Font.Bold = True
bc.HeaderStyle.BackColor = Color.Blue
'bc.ItemStyle.CssClass = "submenu"
bc.ItemStyle.HorizontalAli
gn = HorizontalAlign.Center
bc.DataField = "Price"
bc.DataFormatString = "{0:c}"
idInfoGrid.Columns.Add(bc)
bc = New BoundColumn
bc.HeaderText = " Created "
bc.HeaderStyle.HorizontalA
lign = HorizontalAlign.Center
bc.HeaderStyle.Font.Bold = True
bc.HeaderStyle.BackColor = Color.Blue
'bc.ItemStyle.CssClass = "submenu"
bc.ItemStyle.HorizontalAli
gn = HorizontalAlign.Center
bc.DataField = "DisplayCreated"
idInfoGrid.Columns.Add(bc)
bc = New BoundColumn
bc.HeaderText = " Modified "
bc.HeaderStyle.HorizontalA
lign = HorizontalAlign.Center
bc.HeaderStyle.Font.Bold = True
bc.HeaderStyle.BackColor = Color.Blue
'bc.ItemStyle.CssClass = "submenu"
bc.ItemStyle.HorizontalAli
gn = HorizontalAlign.Center
bc.DataField = "DisplayModified"
idInfoGrid.Columns.Add(bc)
End If
If idInfoGrid.EditItemIndex > -1 Then
idInfoGrid.Columns(6).Visi
ble = False
idInfoGrid.Columns(7).Visi
ble = False
End If
End Sub
Private Sub FillGrid()
' populate the datagrid
Dim cn As SqlClient.SqlConnection
Dim cmd As SqlClient.SqlCommand
Dim sda As SqlClient.SqlDataAdapter
Dim temp_date As System.DateTime, temp_date2 As System.DateTime
cn = CType(Page, WebLibrary.BasePage).Conne
ction("AEM
isc")
Try
If _dt Is Nothing Then
_dt = New DataTable
Else
_dt.Clear()
End If
cmd = cn.CreateCommand
If _mode = Modes.Standard Then
' ElseIf _mode = Modes.ConfirmDelete Then
End If
cmd.CommandText = "SELECT * FROM 888888"
If _filterText.Length > 0 Then
cmd.CommandText += " Where Account " + _filterText
End If
If Len(cmd.CommandText) > 0 Then
sda = New SqlClient.SqlDataAdapter(c
md)
sda.Fill(_dt)
FormatDateField()
FormatStatus()
End If
Finally
If Not sda Is Nothing Then sda.Dispose()
If Not cn Is Nothing Then cn.Dispose()
End Try
End Sub
Private Sub InsertEmpty()
_dt.Rows.InsertAt(_dt.NewR
ow(), 0)
End Sub
Private Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAdd.Click
'inserting()
idInfoGrid.EditItemIndex = 0
' modify text
Dim ecc As WebLibrary.EditCommandColu
mn = DirectCast(idInfoGrid.Colu
mns(0), WebLibrary.EditCommandColu
mn)
ecc.UpdateText = "Insert"
' fill table, insert empty row, bind to datagrid
FillGrid()
InsertEmpty()
idInfoGrid.DataSource = _dt
If _mode = Modes.Standard Then ' no paging needed for delete, can only delete one at a time.
idInfoGrid.AllowPaging = True
idInfoGrid.PageSize = 19
idInfoGrid.PagerStyle.Mode
= PagerMode.NumericPages
idInfoGrid.PagerStyle.CssC
lass = "a"
Else
idInfoGrid.AllowPaging = False
End If
idInfoGrid.DataBind()
End Sub
End Class
Answer : Add new row to datagrid for insertion vb.net
Tried another approach through one of our existing code and it is working fine now.
Thanks for your help.
Random Solutions
Convert a DataReader to a DataSet
gunzip
BSOD on Windows 2000 server Bug Check UNEXPECTED_KERNEL_MODE_TRA<wbr />P (7f)
based on one column value populate another column
how do I change the default name in a comment box in Excel 2007?
Build-in VPN on Windows 7 with Juniper
Vista-Win 2003 Trust Relationship Issue
Windows 7 Backup
SQL datetime issue: The following 2 sections of code produce different results.
Format a portion of text box in bold