Im trying to display data in a datagrid then once a row is selected show a different column of the database in a text box. I use the unique primary key in the datagridview as not visible so once the row is selected, i can get that integer and populate the textbox by using another database query. Of course if there is a way to show the data in the textbox without needed to query the db again that would be great. Could someone please let me know how to get the selected row? Also the rows in the datagridview is set to select the entire row not just a cell if that makes a difference

hi,

Use the below coding:

Private Sub upload_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim sqlcn As New SqlConnection("server=shailu;uid=sa;pwd=;database=master;")
        Dim sadapt As New SqlDataAdapter("select * from abcd", sqlcn)
        Dim ds As DataSet = New DataSet
        sadapt.Fill(ds, "abcd")
        DataGridView1.DataSource = ds.Tables("abcd")
    End Sub


Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
        Dim i, j As Integer
        i = DataGridView1.CurrentRow.Index
        TextBox1.Text = DataGridView1.Item(0, i).Value
        TextBox2.Text = DataGridView1.Item(1, i).Value
        TextBox3.Text = DataGridView1.Item(2, i).Value
        TextBox4.Text = DataGridView1.Item(3, i).Value
    End Sub

Best Regards,
Shailu:)

Im trying to display data in a datagrid then once a row is selected show a different column of the database in a text box. I use the unique primary key in the datagridview as not visible so once the row is selected, i can get that integer and populate the textbox by using another database query. Of course if there is a way to show the data in the textbox without needed to query the db again that would be great. Could someone please let me know how to get the selected row? Also the rows in the datagridview is set to select the entire row not just a cell if that makes a difference

commented: Thanks a lot. +0

Hi, thanks for the help. I actually was able to get it to work using the following:

txtTest.text = Me.DataGridView.SelectedCells(8).Value

where 8 was the value I needed. I'll go thru the code tonight that you posted and try it that way also so thanks again

StatiX

hello
i agree with shailu answer
if shailu we write ur code like this....... will more comfortable

Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
Dim i, j As Integer
i=e.rowIndex
j=e.columnIndex
TextBox1.Text = DataGridView1.Item(i, j).Value
End Sub

Best Regards,:)
Salman

commented: this code doesn't work +0

hello
i agree with shailu answer
if shailu we write ur code like this....... will more comfortable


Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
Dim i, j As Integer
i=e.rowIndex
j=e.columnIndex
TextBox1.Text = DataGridView1.Item(i, j).Value
End Sub

Best Regards,:)
Salman

hi
this code does'nt work well as i was trying .The selected cell is not displayed in the textbox
,the value of other cell is displayed in the textbox.
Deepak

Im trying to display data in a datagrid then once a row is selected show a different column of the database in a text box. I use the unique primary key in the datagridview as not visible so once the row is selected, i can get that integer and populate the textbox by using another database query. Of course if there is a way to show the data in the textbox without needed to query the db again that would be great. Could someone please let me know how to get the selected row? Also the rows in the datagridview is set to select the entire row not just a cell if that makes a difference

Try this one

Private Sub mydatagridview_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles mydatagridview.MouseUp

'we will select first the entire row so that we can simply point the cell value to be displayed in the textboxes

        Dim hit As DataGridView.HitTestInfo = Me.mydatagridview.HitTest(e.X, e.Y)
        If hit.Type = DataGridViewHitTestType.Cell Then
            Me.mydatagridview.ClearSelection()
            Me.mydatagridview.Rows(hit.RowIndex).Selected = True

            Textbox1.Text = Me.mydatagridview.SelectedCells(0).Value
            Textbox2.Text= Me.mydatagridview.SelectedCells(1).Value
            'do the rest
        End If
End Sub

try this code..

Private Sub datagridview1_KeyUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles tblBarang.KeyUp
        Label1.Text = datagridview1.Item(0,tblBarang.CurrentRow.Index).Value
End Sub

this code i want to get value on datagridview1 in first column (0) and the current row...

:)
regard
<<SNIP>>

Hello this dada, I am new in vb 6.0. Now I am doing a project using connecting sting “Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Project.mdb;Persist Security Info=False” now I am completed data entry form. It is working now I want connect a datagrid form nearly it is also completed but I want to view a checkbox in datagrid for “yes or no” what is the code for place the checkbox in datagrid and calendar popup,

Using project.mdb file

Thanking you

Yours sincerely


If any spelling mistakes "sorry........"

hi i had added vales in data grid i need to add the vales and display how to do it i need to select values in previous row and values in current row add then display in current row how to do it plz mail me in srk_india2008@yahoo.com

This is my sample source Code..

Dim i as Integer

For i=0 to DataGridView1.Rows.Count
    INSERT INTO tableName(Field1,Field2) VALUES('" & DataGridView1.Item(0,i).value & "','" & DataGridView1.Item(1,i) & "')"
Next

I hope it will help..

JHPAJIRI

hi ,
i want to insert, update ,delete in gridview also in database.

plssssssss give me reply.
thanks

I give the code for Update and delete. With the help of the code try for insert.

'DESIGN CODE

<asp:GridView ID="GridView1" runat="server" Style="z-index: 100; left: 47px; position: absolute;
            top: 124px" AutoGenerateColumns="False" Height="132px" Width="581px" BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2" >
            <Columns>
	
	'THIS IS THE TEMPLATE CODE WHICH HELPS TO MANIPULATE THE RECORDS IN THE GRID VIEW

            <asp:TemplateField HeaderText="Employee Id">
            <ItemTemplate>    <asp:Label ID="eid1" Text='<%#Eval("eid")%>' runat="server"></asp:Label>            </ItemTemplate>
            <EditItemTemplate>
            <asp:Label ID ="eid" Text='<%#Bind("eid") %>' runat="server" ></asp:Label>
            </EditItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Employee Name">
            <ItemTemplate>            <%#Eval("ename")%>            </ItemTemplate>
            <EditItemTemplate><asp:TextBox ID="ename" Text='<%#Bind("ename") %>' runat="server" ></asp:TextBox></EditItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Address">
            <ItemTemplate>            <%#Eval("eadd")%>            </ItemTemplate>
            <EditItemTemplate><asp:TextBox ID="eadd" Text='<%#Bind("eadd") %>' runat="server"></asp:TextBox></EditItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Phone Number">
            <ItemTemplate>            <%#Eval("ephone")%>            </ItemTemplate>
            <EditItemTemplate><asp:TextBox ID="ephone" Text='<%#Bind("ephone") %>' runat="Server"></asp:TextBox></EditItemTemplate>
            </asp:TemplateField>
                <asp:CommandField ButtonType="Button" HeaderText="Modify" ShowEditButton="True" />
                <asp:CommandField ButtonType="Button" HeaderText="Delete" ShowDeleteButton="True" />
            
            </Columns>
        <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
        <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
        <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
        <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
        <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
        </asp:GridView>


'IMPORTS NAMESPACE FOR THE CONNECTION
Imports System.Data
Imports System.Data.SqlClient



    Dim sadapt1 As SqlDataAdapter
    Dim cn As SqlConnection
    Dim ds As DataTable
    'BIND THE RECORDS ON THE PAGE LOAD
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'SET THE CONNECTION AND BIND TO THE GRIDVIEW
        cn = New SqlConnection("server=shailu;uid=sa;pwd=;database=delete;")
        sadapt1 = New SqlDataAdapter("select * from emp", cn)
        ds = New DataTable
        sadapt1.Fill(ds)
        Session("tosort") = ds.DefaultView
        If Not IsPostBack Then
            GridView1.DataSource = Session("tosort")
            GridView1.DataBind()
        End If
    End Sub

    'SET THE ROWINDEX TO EDIT 
    Protected Sub GridView1_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles GridView1.RowEditing
        GridView1.EditIndex = e.NewEditIndex
        GridView1.DataSource = Session("tosort")
        GridView1.DataBind()
    End Sub

    'TO UPDATE THE RECORD IN THE GRIDVIEW 
    Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView1.RowUpdating
        Dim eid, ename, eadd, ephone As String
        'TO GET THE VALUES FROM THE GRIDVIEW TO MANIPULATE 
        eid = CType(GridView1.Rows(e.RowIndex).FindControl("eid"), Label).Text
        ename = CType(GridView1.Rows(e.RowIndex).FindControl("ename"), TextBox).Text
        eadd = CType(GridView1.Rows(e.RowIndex).FindControl("eadd"), TextBox).Text
        ephone = CType(GridView1.Rows(e.RowIndex).FindControl("ephone"), TextBox).Text

        'TO UPDATE THE RECORD IN THE DATABASE AND ALSO IN THE GRID 
        cn.Open()
        Dim cmd As New SqlCommand
        cmd.CommandText = "update emp set ename='" & ename & "',eadd='" & eadd & "',ephone='" & ephone & "' where eid = '" & eid & "'"
        cmd.Connection = cn
        cmd.ExecuteNonQuery()
        cn.Close()
        GridView1.EditIndex = -1
        GridView1.DataBind()
        sadapt1 = New SqlDataAdapter("select * from emp", cn)
        ds = New DataTable
        sadapt1.Fill(ds)
        Session("tosort") = ds.DefaultView
        GridView1.DataSource = Session("tosort")
        GridView1.DataBind()
    End Sub

    'TO CANCEL THE MANIPULATION 
    Protected Sub GridView1_RowCancelingEdit(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCancelEditEventArgs) Handles GridView1.RowCancelingEdit
        GridView1.EditIndex = -1
        sadapt1 = New SqlDataAdapter("select * from emp", cn)
        ds = New DataTable
        sadapt1.Fill(ds)
        Session("tosort") = ds.DefaultView
        GridView1.DataSource = Session("tosort")
        GridView1.DataBind()
    End Sub

    'TO DELETE THE RECORD IN THE GRIDVIEW AND ALSO IN THE DATABASE 
    Protected Sub GridView1_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles GridView1.RowDeleting
        Dim eid As String
        eid = CType(GridView1.Rows(e.RowIndex).FindControl("eid1"), Label).Text
        cn.Open()
        Dim cmd As New SqlCommand
        cmd.CommandText = "delete from emp where eid = '" & eid & "'"
        cmd.Connection = cn
        cmd.ExecuteNonQuery()
        cn.Close()
        GridView1.EditIndex = -1
        GridView1.DataBind()
        sadapt1 = New SqlDataAdapter("select * from emp", cn)
        ds = New DataTable
        sadapt1.Fill(ds)
        Session("tosort") = ds.DefaultView
        GridView1.DataSource = Session("tosort")
        GridView1.DataBind()
    End Sub

I hope this code may help you.

If you can't understand any of the coding please call me.

Thanks
Shailaja :)

hi ,
i want to insert, update ,delete in gridview also in database.

plssssssss give me reply.
thanks

Hi,

I tried Shailu's code as under and works.

DBVOwner is the DataGrideView

Private Sub DBVOwner_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DBVOwner.CellClick
        Dim v_SelectRow As Integer


        v_SelectRow = Me.DBVOwner.CurrentRow.Index

        txtFirstName.Text = Me.DBVOwner.Item(1, v_SelectRow).Value
        txtMiddleName.Text = Me.DBVOwner.Item(2, v_SelectRow).Value
        txtLastName.Text = Me.DBVOwner.Item(3, v_SelectRow).Value

    End Sub

Thanks Shailu!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.