当我尝试从Sql server数据库获取数据并在GridView中显示时,我得到了以下内容。我的代码如下:
string connstr = @"Data Source=.\SQLEXPRESS; Initial Catalog=Aman;User ID=sa; Password=123";
DataSet ds = new DataSet();
SqlDataAdapter da;
DataTable dt = new DataTable();
SqlConnection conn = new SqlConnection(connstr);
conn.Open();
da = null;
string strsql = "Select File_ID,File_Name,File_Type from Upload_file";
da = new SqlDataAdapter(strsql, conn);
da.Fill(ds, "Upload_file");
GridView1.DataSource = ds.Tables["Upload_file"].DefaultView;
GridView1.DataBind();
答案 0 :(得分:1)
要在视图的网格视图中显示数据,请执行以下操作(ASP.Net MVC razor):
<table>
<tr>
<td>
Heading
</td>
</tr>
@foreach(var item in Model)
{
<tr>
<td>
@* Something that you want to display *@
</td>
</tr>
}
</table>
答案 1 :(得分:0)
如果要对检索到的数据执行特定操作,则应在rowdatabinding / celldatabinding事件中执行此操作,否则应定义列或在标记中或通过代码隐藏 http://msdn.microsoft.com/en-us/library/aa479342.aspx
答案 2 :(得分:0)
试试这个
string connstr = @"Data Source=.\SQLEXPRESS; Initial Catalog=Aman;User ID=sa; Password=123";
SqlConnection conn = new SqlConnection(connstr);
conn.Open();
SqlDataAdapter adapter = new SqlDataAdapter("Select File_ID,File_Name,File_Type from Upload_file ", conn);
DataTable dt = new DataTable();
adapter.Fill(dt);
conn.Close();
GridView1.DataSource = dt;
GridView1.DataBind();
希望这会有所帮助
答案 3 :(得分:0)
最简单的方法可能是:
//Create sql DATA source by passing connection string and select command
SqlDataSource mySqlDataSource= new SqlDataSource("Data Source=.\SQLEXPRESS; Initial Catalog=Aman;User ID=sa; Password=123", "Select File_ID,File_Name,File_Type from Upload_file");
//Assign the data source to GridView data source
GridView1.DataSource = mySqlDataSource;
//Do data binding
GridView1.DataBind();