我想使用datatable插入数据网格并在程序中定义网格列。请帮助我这个代码有什么错? 以下错误出现“无法转换类型System.String对象System.String类型[]”行不插入窗体datagrid
enter code here
SqlConnection connection;
string query;
SqlCommand cmd;
SqlDataAdapter da;
DataSet ds;
DataRow dRows;
public void ViewGrid()
{
connection = new SqlConnection(ConnString);
try
{
connection.Open();
query = @"SELECT * FROM TBLWorkers";
//cmd = new SqlCommand(query, connection);
da = new SqlDataAdapter(query, connection);
ds = new DataSet();
da.Fill(ds, "Workers");
int MaxRows = ds.Tables["Workers"].Rows.Count;
label1.Text = MaxRows.ToString();
dRows = ds.Tables["Workers"].Rows[0];
// Create an unbound DataGridView by declaring a column count.
dataGridView1.ColumnCount = 4;
dataGridView1.ColumnHeadersVisible = true;
// Set the column header names.
dataGridView1.Columns[0].Name = "Recipe";
dataGridView1.Columns[1].Name = "Category";
dataGridView1.Columns[2].Name = "Main Ingredients";
dataGridView1.Columns[3].Name = "Rating";
object[] rows1 = new object[] { dRows[0].ToString(), dRows[1], dRows[2], dRows[3] };
foreach (string[] rowArray in rows1)
{
dataGridView1.Rows.Add(rowArray);
}
}
catch (Exception x)
{
MessageBox.Show(x.Message);
connection.Close();
}
}
如果有任何给定的答案,请接受一些现有的答案,回答了问题! – Coops
哪条线路出现故障? – sq33G