Guest User

Guest User

  • Tech Writer
  • 15
  • 6.2k

How to access a Datatable value from a class to form

Oct 17 2018 9:06 PM
I am new to c#. I have a class called "DBConnection" and a function called "getTransportDate()"
 
Basically, I just want to get the Datatable value from a class and display it a form datagrid view. How to can I achieved this ?
 
What I've done so far:
 
On button click 
  1. private void btnTransport_Click(object sender, EventArgs e)
    {
             DBConnection.getTransportData();
    }

My DBConnection Class function
  1. public DataTable TDataTable = new DataTable();
    public SqlDataAdapter TDataAdapter = new SqlDataAdapter();


  2. public void getTransportData()  
  3. {  
  4. using (SqlCommand cmd = new SqlCommand(@"  
  5. SElECT * FROM IRFormTransport  
  6. ",dbconn))  
  7. {  
  8. cmd.ExecuteNonQuery();  
  9. TDataAdapter.SelectCommand = cmd;  
  10. TDataTable.Clear();  
  11. TDataAdapter.Fill(TDataTable);  
  12. }  
  13. }  
 
My ReportList form
  1. DBConnection DBConnection = new DBConnection();   
  2. private void ReportList_Load(object sender, EventArgs e)  
  3. {  
  4.    ReportGrid.DataSource = DBConnection.TDataTable;  
  5. }  
How can I achieved that ? Thanks.
 

Answers (1)