Datagridview not filling with DataTable records

  • Thread starter Thread starter J Vallee
  • Start date Start date
J

J Vallee

Guest
Total newbie. 2 weeks into learning C#

I have a successful query that returns 3 rows in my test table.

But am unable to insert them into my datagridview

Form comes up blank every time. (this function is called in Form_Load)

This will be a generic form filled with various columns depending on what the user is looking for.


private void BindGrid()
{
string constring= @"Data Source=WIN10\SQLEXPRESS;";
constring = constring + "Initial Catalog=PLV;";
constring = constring + "User id=PLV;";
constring = constring + "Password=PLV;";

DataTable dt = new DataTable();

using (SqlConnection con = new SqlConnection(constring))
{
using (SqlCommand com = new SqlCommand())
{
com.CommandText = "SELECT PrmtNumb, PrmtTNum,NameNumb,ProjDesc FROM dbo.prmtmain WITH(Index(PrmtTNum)) WHERE PrmtNumb = @numb";
com.Connection = con;

using (SqlDataAdapter adapter = new SqlDataAdapter(com))
{
con.Open();
adapter.SelectCommand.Parameters.Add(new SqlParameter("@numb", app.answer));
adapter.Fill(dt);
int cnt = dt.Rows.Count;

// cnt = 3 which is correct

DataRow[] result = dt.Select("PrmtNumb > 0");

// this foreach shows each record correctly.

foreach (DataRow row in result)
{
Console.WriteLine("{0}, {1}", row[0], row[1]);
}
dataGridView1.DataSource = dt;
}
}

}
}

}
}

I suspect there is some setting in the Designer I am missing?

Any ideas/suggestions will be greatly appreciated.

Continue reading...
 
Back
Top