简体   繁体   English

如何通过代码隐藏将列转换为图像列

[英]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 e)
{
    // 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您需要使用 Dispose().... 或类似的方式处理所有一次性对象

    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();
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM