how to save items in gridview and the form name is vendor
rating?

Answer Posted / sree11


Imports System.Data
Imports System.Data.SqlClient
Public Class Rating_Details
Dim da As SqlDataAdapter
Dim vrid, ratingname, qstr As String
Dim cmd As SqlCommand
Dim ds, ds1 As DataSet
Dim i, n As Integer
Dim count As Integer
Public Con As New SqlConnection
("UID=sa;PWD=crea123;Database=purchase;Data Source=CREAINFO-
LHLOQC\SA")
Private Sub Form1_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load
' con.Open()
displaygrid()
Dim col As New DataGridViewTextBoxColumn
col = Dgv_view.Columns(1)
col.MaxInputLength = 15
col = Dgv_view.Columns(2)
col.MaxInputLength = 50
Dgv_view.Columns(2).HeaderText = "RatingName"
For i = 0 To Dgv_view.RowCount - 1
Dgv_view.Item(0, i).Value = "False"
Next
End Sub
'Purpose:Function for Getvalue1
Function Getvalue1(ByVal str As String)
Dim da As SqlDataAdapter
Dim returnvalue As String
returnvalue = ""
Try
da = New SqlDataAdapter(str, con)
ds = New DataSet
da.Fill(ds, 0)
Catch ex As Exception
Finally
End Try
Return ds
End Function
'Purpose: This is a user defined function for printing
datagridview items
Private Function SetupThePrinting() As Boolean
Dim MyPrintDialog As PrintDialog = New PrintDialog()
MyPrintDialog.AllowCurrentPage = False
MyPrintDialog.AllowPrintToFile = False
MyPrintDialog.AllowSelection = False
MyPrintDialog.AllowSomePages = True
MyPrintDialog.PrintToFile = False
MyPrintDialog.ShowHelp = False
MyPrintDialog.ShowNetwork = False
If MyPrintDialog.ShowDialog() <>
System.Windows.Forms.DialogResult.OK Then Return False
myprintdocument.DocumentName = "MeasuringUnits
Report"
myprintdocument.PrinterSettings =
MyPrintDialog.PrinterSettings
myprintdocument.DefaultPageSettings =
MyPrintDialog.PrinterSettings.DefaultPageSettings
myprintdocument.DefaultPageSettings.Margins = New
Printing.Margins(40, 40, 200, 60)
' MyDataGridViewPrinter = New cDataGridViewPrinter
(DataGridView1, myprintdocument, True, True, "Category
Report", New System.Drawing.Font("Tahoma", 18,
FontStyle.Bold, GraphicsUnit.Point), Color.Black, True)
Return True
End Function
'Purpose:Function for Allow Characters
Public Function Allowchars(ByVal Str As String) As
String
Dim str1 As String
= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz12345
67890-_."
Dim str2 As String = Str
If Asc(Str) > 26 Then
If InStr(str1, str2) = 0 Then
Return ""
Exit Function
End If
End If
Return Str
End Function
Dim valid As Boolean
'Purpose:Saving Rating Details in vendor_Ratingdet table
Private Sub Button1_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
Btn_Save.Click
clear_Toolstrip()
Dim ratingname, ratingname1 As String
Dim qstr As String
Dim success, success1, fail As Integer
For count = 0 To Dgv_view.RowCount - 2 ' this loop
for not entering null values in datagridview
If Dgv_view.Rows(count).Cells(0).Value = True
Then
If Dgv_view.Rows(count).Cells(2).Value Is
Nothing Then
Dgv_view.Rows(count).ErrorText
= "Rating name should not be null"
Exit Sub
Else
Dgv_view.Rows(count).ErrorText = ""
End If
ratingname = Trim(Dgv_view.Rows(count).Cells
(2).Value.ToString)

Try 'this try block used when vrid="" then
it calls getvalue1() for inserting new row
vrid = ""
vrid = Dgv_view.Rows(count).Cells
(1).Value.ToString
ds = New DataSet
ds = Getvalue1("select rating_name from
vendor_ratingdet where vr_id<>" & Dgv_view.Rows(count).Cells
(1).Value.ToString.Trim() & "")
valid = True
For count1 As Integer = 0 To ds.Tables
(0).Rows.Count - 1
ratingname1 = ds.Tables(0).Rows
(count1).Item(0).ToString
If UCase(ratingname) = UCase
(ratingname1) Then
valid = False
fail += 1

Exit For
End If
Next
If valid = True Then
'Purpose:for updating record
qstr = "update vendor_ratingdet set
rating_name='" & Dgv_view.Rows(count).Cells
(2).Value.ToString() & "' where vr_id=" & vrid & ""
da = New SqlDataAdapter(qstr, Con)
ds = New DataSet()
da.Fill(ds, "vendor_ratingdet")
success1 += 1
Dgv_view.Rows(count).ErrorText
= "Rating name updated successfully"
End If

Catch ex As Exception
'Purpose:for inserting record and
checking for existing record in GridView
qstr = "select * from vendor_ratingdet"
da = New SqlDataAdapter(qstr, Con)
ds1 = New DataSet()
da.Fill(ds1)
valid = True
For i = 0 To ds1.Tables(0).Rows.Count -
1
If UCase(Trim(ds1.Tables(0).Rows
(i).Item(1))) = UCase(Trim(Dgv_view.Rows(count).Cells
(2).Value)) Then
valid = False
fail += 1
'Dgv_view.Rows(count).ErrorText
= "Rating name already exists"
Exit For
End If
Next
'Purpose:for inserting record in
GridView
If valid = True Then
qstr = "insert into
vendor_ratingdet (rating_name) values ('" & Dgv_view.Rows
(count).Cells(2).Value.ToString.Trim() & "') "
da = New SqlDataAdapter(qstr, Con)
ds = New DataSet()
da.Fill(ds, "vendor_ratingdet")
success += 1
End If
End Try
End If
Next
displaygrid()
If success > 0 Then
stl_Text.Text = success & "Record[s] saved
successfully "
stl_Text.ForeColor = Color.Green
End If
If fail > 0 Then
stl_Text.Text = stl_Text.Text & fail & "Record
[s] Existed"
stl_Text.ForeColor = Color.Red
End If
If success1 > 0 Then
stl_Text.Text = stl_Text.Text & fail & "Record
[s] updated successfully"
stl_Text.ForeColor = Color.Green
End If
End Sub
'Purpose:displaygrid
Private Sub displaygrid()
da = New SqlDataAdapter("select * from
vendor_ratingdet where vr_id<> 0 order by rating_name",
con)
ds = New DataSet()
da.Fill(ds)
Dgv_view.DataSource = ds.Tables(0)
Dgv_view.Columns(1).Visible =
False 'making u_id visible false in
datagridview
Dgv_view.Columns(0).Width = 50
Dgv_view.AlternatingRowsDefaultCellStyle.BackColor
= Color.Bisque
For Each column As DataGridViewColumn In
Dgv_view.Columns
column.SortMode =
DataGridViewColumnSortMode.NotSortable 'making
disable sorting of all columns
Next
End Sub
Private Sub clear_Toolstrip()
stl_Image.Image = Nothing
stl_Text.Text = ""
End Sub
'Purpose:selectall records in DataGridView
Private Sub LinkLabel1_LinkClicked(ByVal sender As
System.Object, ByVal e As
System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles
Lnk_selectAll.LinkClicked
Dim i As Integer
For i = 0 To Dgv_view.RowCount - 1
Dgv_view.Item(0, i).Value = "True"
Next
End Sub
'Purpose:deselectall records in DataGridView
Private Sub LinkLabel2_LinkClicked(ByVal sender As
System.Object, ByVal e As
System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles
Lnk_DeselectAll.LinkClicked
Dim i As Integer
For i = 0 To Dgv_view.RowCount - 1
Dgv_view.Item(0, i).Value = "False"
Next
End Sub
'Purpose:export records to Excel
Private Sub LinkLabel3_LinkClicked(ByVal sender As
System.Object, ByVal e As
System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles
Lnk_Export.LinkClicked
Dim count As Integer = 0
Dim count1 As Integer = 0
For count = 0 To Dgv_view.Rows.Count - 1
If Dgv_view.Item(0, count).Value = True Then
count1 += 1
End If
Next
If count1 > 0 Then
dlg_save.Filter = "Excel files (*.xls)|*.xls"
dlg_save.ShowDialog()
Dim fn As String
fn = dlg_save.FileName
If fn = "" Then
Exit Sub
End If
exporttoexcel_fromdgv(Me, Dgv_view,
fn, "Measuring Units Report")
Else
stl_Text.Text = "Select atleast one record"
stl_Text.ForeColor = Color.Red
End If
End Sub
'for delete the records in datagridview
Private Sub LinkLabel4_LinkClicked(ByVal sender As
System.Object, ByVal e As
System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles
Lnk_Delete.LinkClicked
Dim count As Integer = 0
Dim count1 As Integer = 0
For count = 0 To Dgv_view.Rows.Count - 1
If Dgv_view.Item(0, count).Value = True Then
count1 += 1
End If
Next
If count1 > 0 Then
deletefromdb_datagridview(Me,
Dgv_view, "vendor_ratingdet", "vr_id", 1)
Else
stl_Text.Text = "Select atleast one record"
stl_Text.ForeColor = Color.Red
' stl_Image.Image = My.Resources.Red
End If
displaygrid()
End Sub
Private Sub LinkLabel6_LinkClicked(ByVal sender As
System.Object, ByVal e As
System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles
Lnk_Print.LinkClicked
clear_Toolstrip() 'calling userdefined
function to clear status bar
PrintDGV.Print_DataGridView(Dgv_view)
End Sub
Private Sub LinkLabel5_LinkClicked(ByVal sender As
System.Object, ByVal e As
System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles
Lnk_Refresh.LinkClicked
clear_Toolstrip() 'calling userdefined
function to clear status bar
displaygrid()
End Sub

Private Sub LinkLabel1_Leave(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
Lnk_selectAll.Leave
clear_Toolstrip() 'calling userdefined
function to clear status bar
End Sub
Private Sub LinkLabel2_Leave(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
Lnk_DeselectAll.Leave
clear_Toolstrip() 'calling userdefined
function to clear status bar
End Sub
Private Sub LinkLabel3_Leave(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
Lnk_Export.Leave
clear_Toolstrip() 'calling userdefined
function to clear status bar
End Sub

Private Sub LinkLabel4_Leave(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
Lnk_Delete.Leave
clear_Toolstrip() 'calling userdefined
function to clear status bar
End Sub

Private Sub LinkLabel6_Leave(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
Lnk_Print.Leave
clear_Toolstrip() 'calling userdefined
function to clear status bar
End Sub

Private Sub LinkLabel5_Leave(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
Lnk_Refresh.Leave
clear_Toolstrip() 'calling userdefined
function to clear status bar
End Sub
Private Sub Button1_Leave(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
Btn_Save.Leave
clear_Toolstrip() 'calling userdefined
function to clear status bar
End Sub
Private Sub DataGridView1_CellValueChanged(ByVal sender
As System.Object, ByVal e As
System.Windows.Forms.DataGridViewCellEventArgs) Handles
Dgv_view.CellValueChanged
Try
If e.ColumnIndex <> 0 Then
Dgv_view.CurrentRow.Cells(0).Value = True
End If
Catch ex As Exception
End Try
End Sub
Private Sub Button2_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
Btn_Close.Click
Me.Close()
End Sub
Private Sub DataGridView1_EditingControlShowing(ByVal
sender As Object, ByVal e As
System.Windows.Forms.DataGridViewEditingControlShowingEventA
rgs) Handles Dgv_view.EditingControlShowing
Dim tb As TextBox
tb = CType(e.Control, TextBox)
If Dgv_view.CurrentCell.ColumnIndex = 2 Then
'---add an event handler
AddHandler tb.KeyPress, AddressOf
TextBox_KeyPress
Else
RemoveHandler tb.KeyPress, AddressOf
TextBox_KeyPress
End If
End Sub
Private Sub TextBox_KeyPress(ByVal sender As
System.Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs)
e.KeyChar = Allowchars(e.KeyChar)
End Sub



End Class

Is This Answer Correct ?    1 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

So you know which dll is used for microsoft .net run time?

509


What are the assembly entry points?

574


Why is the xml infoset specification different from the xml dom?

514


What is DLL HELL in VB.NET

606


Name some of the features present in vb 2005?

564






Explain the difference between thread and process?

504


What is a stream in vb.net?

519


How would you implement inheritance using vb.net?

505


Is vb.net dead?

517


What are the different types of Lock available in Visual Basic?

630


Described strong typing and weak typing?

619


What is the main purpose of garbage collector?

502


Explain nested classes?

554


Explain the difference between vb 6 and vb.net?

536


Name a feature which is common to all .net languages?

573