C# 无法通过数据集在gridview上显示microsoft sql server中的项
我无法显示添加到SQL数据库中的值。这是我用过的代码。最初的想法是当用户进入特定页面时,默认情况下,它将以gridview的形式显示所有值。因此,我将SQL连接代码放在页面加载中。我已经在服务器浏览器上测试了我的连接,它显示ping测试成功 我的SQL连接C# 无法通过数据集在gridview上显示microsoft sql server中的项,c#,asp.net,sql-server,visual-studio,C#,Asp.net,Sql Server,Visual Studio,我无法显示添加到SQL数据库中的值。这是我用过的代码。最初的想法是当用户进入特定页面时,默认情况下,它将以gridview的形式显示所有值。因此,我将SQL连接代码放在页面加载中。我已经在服务器浏览器上测试了我的连接,它显示ping测试成功 我的SQL连接 protected void Page_Load(object sender, EventArgs e) { if(Page.IsPostBack == false) { SqlCon
protected void Page_Load(object sender, EventArgs e)
{
if(Page.IsPostBack == false)
{
SqlConnection connSel = new SqlConnection("Data Source = localhost;" + "Initial Catalog = project; Integrated Security = SSPI");
SqlDataAdapter adapSel;
string mySQL = "Select * from Report";
adapSel = new SqlDataAdapter(mySQL, connSel);
connSel.Open();
DataSet dsSel = new DataSet();
adapSel.Fill(dsSel);
GWCase.DataSource = dsSel;
GWCase.DataBind();
connSel.Close();
}
}
这是我的gridview的源代码
<asp:GridView ID="GWCase" runat="server" AutoGenerateColumns="False" Width="100%" BackColor="#CCCCCC" BorderColor="#999999" BorderStyle="Solid" BorderWidth="3px" CellPadding="4" CellSpacing="2" ForeColor="Black" Height="199px" AutoGenerateSelectButton="True" OnSelectedIndexChanged="GWCase_SelectedIndexChanged">
<FooterStyle BackColor="#CCCCCC" />
<HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#CCCCCC" ForeColor="Black" HorizontalAlign="Left" />
<RowStyle BackColor="White" />
<SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#808080" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#383838" />
</asp:GridView>
你能试试这个吗?使用前只需打开连接即可
protected void Page_Load(object sender, EventArgs e)
{
if(Page.IsPostBack == false)
{
SqlConnection connSel = new SqlConnection("Data Source = localhost;" + "Initial Catalog = project; Integrated Security = SSPI");
connSel.Open();
SqlDataAdapter adapSel;
string mySQL = "Select * from Report";
adapSel = new SqlDataAdapter(mySQL, connSel);
DataSet dsSel = new DataSet();
adapSel.Fill(dsSel);
GWCase.DataSource = dsSel;
GWCase.DataBind();
connSel.Close();
}
}
这是因为gridview
AutoGenerateColumns=“False”
不会自动生成列。在网格视图代码中,我没有看到它为其生成列的任何BoundField
或TemplateField
,即网格视图没有任何列可显示。因此,最好设置AutoGenerateColumns=“true”
或定义一些BoundField
或TemplateField
。我想它会比。在我开始处理表和数据集之前的某个时候更有效。我不确定,但我认为每个数据集都包含许多表,当您尝试从数据库中选择一个特定的表并填充时,它存储在数据集的第一个表中。尝试将数据源设置为:
GWCase.DataSource = dsSel.Tables[0];
还是一样。my gridview上未显示任何值。请尝试在sql中使用您的查询。看看是否有返回值。