Add newrow from gridview to another gridview - devexpress

On Form2.GridView1 i click repository button.the selected row will be transmitted at Form1.GridView1.
This code work very well but still i can send a single row. I want if I click the first row in Form2.GridView1 will be transmitted at Form1.GridView1 .. and if as an example I choose a second row I will have in Form1.gridview1 2 rows and so on.
My code :
Private Sub RepositoryItemButtonEdit1_Click(sender As Object, e As EventArgs) Handles RepositoryItemButtonEdit1.Click
Dim dt As DataTable = New DataTable()
Dim Dr As DataRow = dt.NewRow()
dt.Columns.Add(New DataColumn("Column1"))
dt.Columns.Add(New DataColumn("Column2"))
dt.Columns.Add(New DataColumn("Column3"))
Dr("Column1") = GridView1.GetFocusedRowCellValue("Id")
Dr("Column2") = GridView1.GetFocusedRowCellValue("Name")
Dr("Column3") = GridView1.GetFocusedRowCellValue("Surname")
dt.Rows.Add(Dr)
Form1.GridControl1.DataSource = dt
Me.Close()
End Sub

Related

How to delete a row in gridview that was added dynamically using asp.net and vb.net

I have a gridview. the rows are added dynamically
Protected Sub btnAddCustomer_Click(sender As Object, e As EventArgs) Handles btnAddCustomer.Click
Dim dtSource As New DataTable
dtSource = CType(ViewState("dtSource"), DataTable)
Dim dr As DataRow = dtSource.NewRow()
dr("Id") = DropDownList3.SelectedIndex
dr("Name") = DropDownList3.SelectedItem
dtSource.Rows.Add(dr)
ViewState("dtSource") = dtSource
GridView1.DataSource = dtSource
GridView1.DataBind()
GridView1.HeaderRow.Cells(1).Text = "ID"
GridView1.HeaderRow.Cells(2).Text = "NAME"
End Sub
Each row has a delete image button, and when it is clicked it fires the Code:
Protected Sub GridVeiw1_RowCommand(sender As Object, e As GridViewCommandEventArgs) Handles GridView1.RowCommand
If ViewState("dtSource") IsNot Nothing Then
Dim dtSource As New DataTable
Dim drCurrentRow As DataRow = Nothing
Dim rowIndex As Integer = Convert.ToInt32(e.CommandArgument)
dtSource = DirectCast(ViewState("dtSource"), DataTable)
If dtSource.Rows.Count > 0 Then
dtSource.Rows.RemoveAt(rowIndex)
drCurrentRow = dtSource.NewRow()
dtSource.AcceptChanges()
ViewState("dtSource") = dtSource
GridView1.DataSource = dtSource
GridView1.DataBind()
End If
End If
End Sub
The delete procedure is not working!!!
Any suggessions Please?
I read the question regarding How to delete a row in GV, but it is not helping because am not deleting from GV but from DataTable that contains the data for the GV

How to sum one column in gridview and show it in a textbox

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim constr As String = ConfigurationManager.ConnectionStrings("mcbdatabseConnectionString").ConnectionString
Using con As New MySqlConnection(constr)
Using cmd As New MySqlCommand("SELECT * FROM mcbdatabse.subject_enrolled")
Using sda As New MySqlDataAdapter()
cmd.Connection = con
sda.SelectCommand = cmd
Using dt As New DataTable()
sda.Fill(dt)
GridView1.DataSource = dt
GridView1.DataBind()
End Using
End Using
End Using
End Using
End If
End Sub
I have this code for my gridview, I want to total all the units and show it on my textbox;txtTotalUnits Thanks.This is my gridview and textbox
You could add an event handler for the GridView.RowDataBound Event as follows
Dim total as Integer
Sub CustomersGridView_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles CustomersGridView.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
total = total + Convert.ToInt32(e.Row.Cells(the_column_index).Text) .
txtTotalUnits.Text = Convert.ToString(total)
End If
End Sub
More similar examples on MSDN
Use the datasource table, you need using System.Linq;
var result = tbl.AsEnumerable().
Sum(x => Convert.ToDecimal(x["ColumnNameForWhichYouWantSum"].ToString()));
txtTotalUnits.Text = result.ToString();
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim constr As String = ConfigurationManager.ConnectionStrings("mcbdatabseConnectionString").ConnectionString
Using con As New MySqlConnection(constr)
Using cmd As New MySqlCommand("SELECT * FROM mcbdatabse.subject_enrolled")
Using sda As New MySqlDataAdapter()
cmd.Connection = con
sda.SelectCommand = cmd
Using dt As New DataTable()
sda.Fill(dt)
Dim total As Integer = 0
For i As Integer = 0 To dt.Rows.Count - 1
Dim drw As DataRow = dt.Rows(i)
Dim units As Integer = CInt(drw("Units")) 'Assuming Units is the column name'
total += units
Next i
txtTotalUnits.Text = total.ToString
GridView1.DataSource = dt
GridView1.DataBind()
End Using
End Using
End Using
End Using
End If
End Sub

Dropdown not firing event on usercontrol

I have a dropdown list which is inside a user control on my page.
When I select an item in a gridview it fills some data including this dropdown list selecting the correspondent value to the gridview row.
Then I can change the selection, but when i try to do that the Selected Index Changed event isn't firing. At least not at first. If i select again it fires.
I'm using asp.net with VB.
I have put AutoPostback = true, I've set the Handles in the codebehind method, I've set a default empty line and set it to default selection, but I can't get it to work.
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
If Page.IsPostBack() Then
fillProd()
Dim cert As String = Session("var")
isFilled = Session("isFilled")
If Not IsNothing(cert) And Not isFilled Then
FillFields(cert)
End If
End If
End Sub
Private Sub fillProd()
dt = HttpContext.Current.Session("dt")
Dim row() As DataRow = dt.Select()
Dim dtProductos As New DataTable()
dtProductos.Columns.Add(New DataColumn("valorcombo", GetType(String)))
dtProductos.Columns.Add(New DataColumn("textocombo", GetType(String)))
Dim firstrow As DataRow = dtProductos.NewRow()
firstrow (0) = 0
firstrow (1) = ""
dtProductos.Rows.Add(firstrow)
For i As Integer = 0 To row.Length - 1
Dim fila As DataRow = dtProductos.NewRow()
fila(0) = row(i)("cod")
fila(1) = row(i)("text")
dtProductos.Rows.Add(fila)
Next
ddlproductos.DataSource = dtProductos
ddlproductos.DataTextField = "textocombo"
ddlproductos.DataValueField = "valorcombo"
ddlproductos.DataBind()
ddlproductos.SelectedIndex = 0
End Sub
Private Sub FillFields(cert As String)
Dim dtSubsData As DataTable = GetData(cert)
If Not IsNothing(dtSubsData) Then
Dim row As DataRow = dtSubsData.Rows(0)
ddlproductos.ClearSelection()
ddlproductos.SelectedValue = row("cod").ToString()
Session("isFilled") = True
isFilled = Session("isFilled")
End If
End Sub
Can anyone help?

Adding gridview line data into textboxes and labels

I'm trying to get the gridview details to be put into textboxes for better view and edit.
I'm listing the following code to create the gridview:
'Finds all cases that are not closed
Protected Sub listAllCases()
sqlCommand = New SqlClient.SqlCommand("SELECT TC.caseId,TS.subName,TSU.userName,TC.caseType,TC.caseRegBy,TC.caseTopic,TC.caseDesc,TC.caseSolu,TC.caseDtCreated, TC.caseStatus FROM TBL_CASE TC INNER JOIN TBL_SUBSIDIARY_USER TSU ON TC.caseUser = TSU.userID INNER JOIN TBL_SUBSIDIARY TS on TSU.usersubId = TS.subId WHERE TC.caseStatus = 0 order by caseId")
sqlCommand.Connection = sqlConnection
sqlConnection.Open()
'sqlCommand.Parameters.AddWithValue("#subID", Me.caseSub.SelectedItem.Value)
Dim dr As SqlClient.SqlDataReader
dr = sqlCommand.ExecuteReader
If dr.HasRows Then
allCases.DataSource = dr
allCases.DataBind()
Else
allCases.DataSource = Nothing
allCases.DataBind()
End If
dr.Close()
sqlConnection.Close()
End Sub
Then I use the a function on the gridview onselectindexchanged and Writes this:
Protected Sub OnSelectedIndexChanged(sender As Object, e As EventArgs)
Dim row As GridViewRow = allCases.SelectedRow
txtcase.Text = row.Cells(1).Text()
txtsub.Text = row.Cells(2).Text
txtuser.Text = row.Cells(3).Text
oDato.Text = row.Cells(9).Text
lDato.Text = "Saken er ikke lukket!"
txttype.Text = row.Cells(4).Text.ToString
txtregBy.Text = row.Cells(5).Text.ToString
txttopic.Text = row.Cells(6).Text
txtDesc.Text = row.Cells(7).Text
txtSolu.Text = row.Cells(8).Text
lblinfo.Text = row.Cells(6).Text
End Sub
I only get it to display cells 1 to 9. Means cell 4 to 8 is not listed or being blank, even though i know it should contain data.
Any tips or Clues is very appreciated!
Like This ?
Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As
TextBox1.text = DataGridView1.Rows(e.Index).Cells(0).value.toString
TextBox2.text = DataGridView1.Rows(e.Index).Cells(1).value.toString
TextBox3.text = DataGridView1.Rows(e.Index).Cells(2).value.toString
TextBox4.text = DataGridView1.Rows(e.Index).Cells(3).value.toString
TextBox5.text = DataGridView1.Rows(e.Index).Cells(4).value.toString
TextBox6.text = DataGridView1.Rows(e.Index).Cells(5).value.toString
TextBox7.text = DataGridView1.Rows(e.Index).Cells(6).value.toString
TextBox8.text = DataGridView1.Rows(e.Index).Cells(7).value.toString
TextBox9.text = DataGridView1.Rows(e.Index).Cells(8).value.toString
TextBox10.text = DataGridView1.Rows(e.Index).Cells(9).value.toString
End Sub
This will display all the information in the row by clicking the record...
e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
sorry for that ...
by clicking the data the (e.Index) will locate in which DataGridView.Rows(), then the cells.(n) and etc... will get your data
ok. i got it right, finally. the code i used to get it work is as follows:
Protected Sub allCases_OnSelectedIndexChanged(sender As Object, e As EventArgs)
Dim row As GridViewRow = allCases.SelectedRow
txtcase.Text = row.Cells(1).Text()
txtsub.Text = row.Cells(2).Text.ToString
txtuser.Text = row.Cells(3).Text
oDato.Text = row.Cells(9).Text
txtDesc.Text = TryCast(row.FindControl("lblcaseDesc"), Label).Text
txtSolu.Text = TryCast(row.FindControl("lblcaseSolu"), Label).Text
end sub
this means I need to fetch the boundfields and templatefields differently.
Anyhow thanks for the reponse.

ASP.Net Gridview paging of columns

I am using ASP gridview in my program to show the records of the employees. The data contains about 60 columns. I have converted the columns into rows using the following code
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
FillGrid1()
End If
End Sub
Public Sub FillGrid1()
Dim strUnit As String
Dim intMonthYr As Integer
strUnit = Session("Unit")
intMonthYr = Session("MonthYr")
objReports.Unit = strUnit
objReports.ForMonth = intMonthYr
ds = objReports.MonthlyPayBill
Dim new_ds As New DataSet
new_ds = FlipDataSet(ds)
Dim my_DataView As DataView = new_ds.Tables(0).DefaultView
GridView1.DataSource = my_DataView
GridView1.DataBind()
End Sub
Public Function FlipDataSet(ByVal my_DataSet As DataSet) As DataSet
Dim ds1 As New DataSet
Dim table As New DataTable()
For Each dt As DataTable In my_DataSet.Tables
For i As Integer = 0 To dt.Rows.Count
table.Columns.Add(Convert.ToString(i))
Next
Dim r As DataRow
For k As Integer = 0 To dt.Columns.Count - 1
r = table.NewRow()
r(0) = dt.Columns(k).ToString()
For j As Integer = 1 To dt.Rows.Count
r(j) = dt.Rows(j - 1)(k)
Next
table.Rows.Add(r)
Next
ds1.Tables.Add(table)
Next
Return ds1
End Function
The 60 columns are placed as 60 rows and each person is represented as a column.There are about 50 person's record and I want to go for paging in the columns to show 10 persons in each page rather than 10 field in the gridview.

Resources