AlexJames
Well-known member
- Joined
- Mar 20, 2020
- Messages
- 65
- Programming Experience
- 10+
Hi All
I'm busy building a C# WinForms app and have a method on my form that gets data from the database and populates a DataGridView. This is working nicely, but i want to move the code form my form to it's own method in another class so that i can reuse it.
The above code works perfectly on my form, but i'm struggling to figure out how I would call it if this method was in a class, do i load the results into a list and then bind my grid to that list ? Any suggestions or guidance on this would b greatly appreciated.
Many thanks
AJ
I'm busy building a C# WinForms app and have a method on my form that gets data from the database and populates a DataGridView. This is working nicely, but i want to move the code form my form to it's own method in another class so that i can reuse it.
C#:
private void PopulateGrid()
{
dgvExchanges.Rows.Clear();
DatabaseConnection myDbConn = new DatabaseConnection();
SQLiteDataReader dr = myDbConn.ReadData("SELECT * " +
"FROM ExchangeSetup " +
"ORDER BY ExchangeName");
int newRow = -1;
while (dr.Read())
{
dgvExchanges.Rows.Add();
if (newRow == -1)
{
newRow = 0;
}
else
{
newRow += 1;
}
dgvExchanges.Rows[newRow].Cells[0].Value = dr["ExchangeName"].ToString();
dgvExchanges.Rows[newRow].Cells[1].Value = dr["ExchangeAPIKey"].ToString();
dgvExchanges.Rows[newRow].Cells[2].Value = dr["ExchangeSecret"].ToString();
dgvExchanges.Rows[newRow].Cells[3].Value = dr["ExchangeID"].ToString();
}
myDbConn.CloseConnection();
var ExchangeCount = dgvExchanges.Rows.Count;
lblExchangeCount.Text = string.Concat(ExchangeCount);
}
The above code works perfectly on my form, but i'm struggling to figure out how I would call it if this method was in a class, do i load the results into a list and then bind my grid to that list ? Any suggestions or guidance on this would b greatly appreciated.
Many thanks
AJ