Add rows to datatable with a button asp.net VB? - asp.net

I am wondering how to add rows to an already populated datatable with a button. the code I have so far is this...
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim dt As New DataTable("Dt")
dt.Columns.Add("test1")
dt.Columns.Add("test2")
dt.Columns.Add("test3")
GridView1.DataSource = dt
GridView1.DataBind()
End Sub
Protected Sub btnadd_Click(sender As Object, e As EventArgs) Handles btnadd.Click
Dim dt As New DataTable
dt.Rows.Add(TextBox1.Text, TextBox2.Text, TextBox3.Text)
GridView1.DataSource = dt
GridView1.DataBind()
End Sub

it works for me. Because am I storing the DataTable in ViewState so that I can reference the current data associated within the DataTable when it postback.
You are initializing DataTable in Page_Load method including header of DataTable. When you are clicking on btnadd button then you are not getting existing DataSource. If you assign dt in ViewState("DataSource ") in Page_Load method and declare again DataTable in btnAdd_Click method like Dim dt As DataTable = DirectCast(ViewState("DataSource "), DataTable) then it will work because previous DataSource you are getting now. Finally I want to say that DataSource doesn't remember the DataSource you set in a previous rendering of the page.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Dim dt As New DataTable("Dt")
dt.Columns.Add("test1")
dt.Columns.Add("test2")
dt.Columns.Add("test3")
GridView1.DataSource = dt
GridView1.DataBind()
ViewState("DataSource ") = dt
End Sub
Protected Sub btnadd_Click(sender As Object, e As EventArgs) Handles btnadd.Click
Dim dt As DataTable = DirectCast(ViewState("DataSource "), DataTable)
GridView1.DataSource = dt
GridView1.DataBind()
dt.Rows.Add("Hi1", "Hi2", "Hi2")
GridView1.DataSource = dt
GridView1.DataBind()
End Sub

Related

How to add linkbutton control to table cell dynamically

how to add linkbutton control to table cell dynamically and its event handler .?
Class SurroundingClass
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
Dim tbl As HtmlTable = New HtmlTable()
Dim tr As HtmlTableRow = New HtmlTableRow()
Dim td As HtmlTableCell = New HtmlTableCell()
tr.Cells.Add(td)
tbl.Rows.Add(tr)
Dim lbtn As LinkButton = New LinkButton()
lbtn.Text = "My Link Button"
lbtn.Click += New EventHandler(AddressOf LinkButton1_Click) '<- THIS LINE ERROR
td.Controls.Add(lbtn)
End Sub
Protected Sub LinkButton1_Click(ByVal sender As Object, ByVal e As EventArgs)
End Sub
End Class
ERROR message image
Ah, you are using the C# format for attaching the event "+=", in VB use the AddHandler method.
For Example:
AddHandler lbtn.Click, AddressOf LinkButton1_Click

Can i add new row to datagrid view with a button click in ASP.Net written VB.net

let say i have 2 text box, 1 button and 1 gridview and i want to add data from textboxs to gridview with a button click.
How can I achive it.
I have tried below code but it replaced the old row.
Private dt As New DataTable
Private Sub Btnidadd_Click(sender As Object, e As EventArgs) Handles Btnidadd.Click
dt.Columns.Add("First Name")
dt.Columns.Add("Last Name")
Dim R As DataRow = dt.NewRow
R("First Name") = textbox1.Text
R("Last Name") = textbox2.Text
dt.Rows.Add(R)
GridView1.DataSource = dt
GridView.DataBind()
When you click the button in ASP.NET, a postback occurs and the page is recreated.
So, you need to keep data somewhere.
For example, if you save it to a session variable like this.
Private dt As DataTable
Private Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not IsPostBack Then
dt = New DataTable
dt.Columns.Add("First Name")
dt.Columns.Add("Last Name")
Session("dt") = dt
Else
dt = CType(Session("dt"), DataTable)
End If
End Sub
Private Sub Btnidadd_Click(sender As Object, e As EventArgs) Handles Btnidadd.Click
Dim R As DataRow = dt.NewRow
R("First Name") = TextBox1.Text
R("Last Name") = TextBox2.Text
dt.Rows.Add(R)
GridView1.DataSource = dt
GridView1.DataBind()
Session("dt") = dt
End Sub

Why is Response.Write not binding?

I'm stuck with code that is failing to bind. It's not using SQL, it's just a simple form using Response.Write in the codebehind.
Here's the code:
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not IsPostBack Then
'Populate DataTable
Dim dt As New DataTable()
dt.Columns.Add("fname")
dt.Columns.Add("lname")
dt.Rows.Add()
dt.Rows(0)("fname") = Response.ContentType = "fname"
dt.Rows(0)("lname") = Response.ContentType = "lname"
'Bind Datatable to Labels
lblfname.Text = dt.Rows(0)("fname").ToString()
lbllname.Text = dt.Rows(0)("lname").ToString()
End If
End Sub

ASP.NET VB Color single Row in Gridview

I want to color single Rows when there empty in a Gridview, but it changes the color from every Row empty and not.
Private Sub _Default_Load(sender As Object, e As EventArgs) Handles Me.Load
... SQL Connection Stuff
Using dt As New DataTable()
sda.Fill(dt)
GridView1.DataSource = dt
GridView1.DataBind()
Dim Menge As Integer = GridView1.Rows.Count - 1
For i = 0 To Menge
For Each row As GridViewRow In GridView1.Rows
If GridView1.Rows(i).Cells(4).Text = "" Then
GridView1.Rows(i).Cells(4).BackColor = System.Drawing.Color.Red
End If
Next
Next
End Sub
Can someone Help me?
You should use the following code in your Default_Load event, but this will only color rows when page loads. Ideally, you should hook up the RowDataBound event with your grid view.
Code if using Default_Load event
Private Sub _Default_Load(sender As Object, e As EventArgs) Handles Me.Load
... SQL Connection Stuff
Using dt As New DataTable()
sda.Fill(dt)
GridView1.DataSource = dt
GridView1.DataBind()
'color rows based on some condition
For Each row As GridViewRow In GridView1.Rows
If row.Cells(4).Text = "" Then
row.Cells(4).BackColor = System.Drawing.Color.Red
End If
Next
End Sub
Code if using RowDataBound event
Protected Sub GridView1_RowDataBound(sender As Object, e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
If e.Row.Cells(4).Text = "" Then
e.Row.Cells(4).BackColor = System.Drawing.Color.Red
End If
End If
End Sub

How to keep DataTable persistent?

I have a Gridview filled by a dataTable, filled by a DataAdapter. That's how grid is initially loaded in Page_Load.
To add a search funcionality, I make the same but passing TextBox.Text as parameter to the SELECT... LIKE ... statement.
To add an edit funcionality(a button in every row) I need the previous data in the dataTable, and if I performed a search before editing I need only the result of the search in my dataTable.
The problem is, I don't know how to keep its value alive (persistent), and dataTable has 0 columns when I press teh edit button, so it doesn't display anything to edit.
I guess it happens because I'm using Using, and dataTable is probably getting cleaned after End Using.
In that case, whta can I do to fix it? I thought removing miconn.Close() but it doesn't solve anything, in fact, I don't know if connection is still open after End Using.
Code:
Dim con As New Connection
Dim table As New DataTable()
Private Sub fill_grid()
Using miconn As New SqlConnection(con.GetConnectionString())
Dim sql As String = "SELECT area,lider_usuario FROM AREA"
Using command As New SqlCommand(sql, miconn)
Using ad As New SqlDataAdapter(command)
ad.Fill(table)
GridView1.DataSource = table
GridView1.DataBind()
'miconn.Close()
End Using
End Using
End Using
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
fill_grid()
End If
End Sub
Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearch.Click
Dim miCon As New Connection
Using miconn As New SqlConnection(miCon.GetConnectionString())
Dim sql As String = "SELECT area,lider_usuario FROM AREA WHERE area LIKE #area"
Using command As New SqlCommand(sql, miconn)
command.Parameters.Clear()
command.Parameters.Add("#area", SqlDbType.VarChar).Value = "%" + TextBox1.Text + "%"
Using ad As New SqlDataAdapter(command)
ad.Fill(table)
GridView1.DataSource = table
GridView1.DataBind()
'miconn.Close()
End Using
End Using
End Using
End Sub
Protected Sub EditRow(ByVal sender As Object, ByVal e As GridViewEditEventArgs)
GridView1.EditIndex = e.NewEditIndex
GridView1.DataSource = table
GridView1.DataBind()
End Sub
Protected Sub CancelEditRow(ByVal sender As Object, ByVal e As GridViewCancelEditEventArgs)
GridView1.EditIndex = -1
GridView1.DataSource = table
GridView1.DataBind()
End Sub
BindGrid()
{
var dt = YourMethodReturningDatatable();
ViewState["Table"] = dt;
grid.DataSource = ViewState["Table"];
grid.Databind();
}
page_load
{
if(not ispostback) // not because my 1 key stopped working.
{
BindGrid();
}
}
I would suggest you to make two different functions for filling the data table and binding it. Call the filling function before the !IsPostBack condition and do the binding inside the condition. Here is the sample code:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim table as new DataTable
table = GetData() 'The function that would return a data table
If Not IsPostBack Then
GridView1.DataSource = table
GridView1.DataBind()
End If
End Sub
Private Function GetData() As DataTable
Using miconn As New SqlConnection(con.GetConnectionString())
Dim sql As String = "SELECT area,lider_usuario FROM AREA"
Using command As New SqlCommand(sql, miconn)
Using ad As New SqlDataAdapter(command)
ad.Fill(table)
GetData = table
miconn.Close()
End Using
End Using
End Using
End function
Hope it helps.

Resources