How to convert a column into an image column through the code-behind
i have a grid view that is filled through a data-source from the code behind :
protected void Page_Load(object sender, EventArgs 开发者_如何学Pythone)
{
// filling the grid view
MainGrid.DataSource = Update();
MainGrid.DataBind();
}
protected DataSet Update()
{
SqlConnection conn = new SqlConnection(@"ConnectionString");
SqlCommand cmd = new SqlCommand("SELECT tim,com,pic FROM ten", conn);
conn.Open();
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
return ds;
}
but i have a file upload that inserts the file-path into the database (and it works fine), but i would like to know how to change the column type to image through the code-behind.
thanks
The answer is - from the comments - set your column types in the declaration of the grid, and bind your data in the code behind.
If you need variable column types, the simplest route is to include multiple columns, and show and hide them appropriately.
You need to dispose all the disposable objects using the Dispose()....or like
using (SqlConnection conn = new SqlConnection(@"ConnectionString"))
{
using (SqlCommand cmd = new SqlCommand("SELECT tim,com,pic FROM ten", conn))
{
conn.Open();
using (DataSet ds = new DataSet())
{
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
da.Fill(ds);
MainGrid.DataSource = ds;
}
}
conn.Close();
}
}
精彩评论