locked
How to show data in lable with Dataset? RRS feed

  • Question

  • Hi all
    I'm new in ADO.net.
    I have a windows form with Datagrid and lable.I can show data from database in my datagrid but i want to show data in my lable .that means when i scroll my datagrid ,for xample name field of each rows show in lable .how can i do that?
    here is my code but it doesnt work correctly
     
    Code Snippet

    SqlConnection con=new SqlConnection("......");
    SqlDataAdapter adaptor=new SqlDataAdapter("SELECT * FROM Info",con);
    DataSet ds=new DataSet();
    adaptor.Fill(ds,"Table_Info");          
    dataGridView1.DataSource=ds.Tables["Table_Info"];
    label1.Text = ds.Tables["Table_Info"].Columns["name"].Tostring();



    Thank you
    Babak
    Tuesday, April 15, 2008 6:05 PM

Answers

  • First, You'll need to get the index of the currently selected record.

     

    then:

     

    Code Snippet

     

    int currentIndex = dataGridView1.CurrentRow.Index

    label1.Text = ds.Tables["Table_Info"].Rows[currentIndex]["name"].Tostring();

     

     

     

     

    Hope this helps...

     

    Tuesday, April 15, 2008 8:36 PM