I have a process which loads a DataGridView from a TSql view. There has been a debate amongst my users on whether to return all the rows or a certain number (im in the latter camp: TOP 100). I decided that the best approach would be to return the number of rows dynamically based on a field set by the user. I was expecting this to be a fairly trivial task. I was expecting to load a datatable with a while loop and a read function and stop at a certain number of rows. Is this possible? Is there a huge benefit in keeping the code as it is below, but cutting the datatable (dt) down to the desired number of rows and then loading the datagridview from there?
public void Load_DGV(DataGridView dgv, string sqlx)
{
DateTime d1 = DateTime.Now;
SqlCommand command = new SqlCommand(sqlx.ToString());
SqlConnection connection = new SqlConnection(Properties.Settings.Default._CONNSTRING);
command.Connection = connection;
bool Success = true;
DataTable dt = new DataTable();
connection.Open();
SqlDataReader reader = command.ExecuteReader();
dt.Load(command.ExecuteReader());
connection.Close();
dgv.DataSource = null;
dgv.DataSource = dt;
Color_DGV(dgvProposal);
dgvProposal.Sort(dgvProposal.Columns[m_Current_Menu_Item.OrderByCol], ListSortDirection.Descending);
DateTime d2 = DateTime.Now;
Console.WriteLine("Execution of " + sqlx + " took " + (d2 - d1).Seconds.ToString() + "s. ");
}