ASP.NET C#GridView-可变列数和图像缩略图
使用ASP.NET C#GridView-可变列数和图像缩略图,c#,asp.net,gridview,sql-server-express,C#,Asp.net,Gridview,Sql Server Express,使用GridView1.DataBind()和AutoGenerateColumns=“true”如何查找特定列并显示该URL中图像的缩略图而不是URL GridView是SQL查询的结果,该查询可能返回2到10列(其中一列可能是图像URL) 对于缩略图,我相信可以使用image.GetThumbnailImage(),但在这种情况下,在哪里使用它呢 正在生成SQL查询: if (CheckBoxUser.Checked) search_fields += "UserName"; if (Ch
GridView1.DataBind()
和AutoGenerateColumns=“true”
如何查找特定列并显示该URL中图像的缩略图而不是URL
GridView是SQL查询的结果,该查询可能返回2到10列(其中一列可能是图像URL)
对于缩略图,我相信可以使用image.GetThumbnailImage()
,但在这种情况下,在哪里使用它呢
正在生成SQL查询:
if (CheckBoxUser.Checked) search_fields += "UserName";
if (CheckBoxDate.Checked) search_fields += ",EventDate";
...
if (CheckBoxImageUrl.Checked) search_fields += ",ImageUrl";
SqlConnection conn = new SqlConnection(connectionString);
string select = "SELECT " + search_fields + " FROM TableName";
SqlDataAdapter DataCommand = new SqlDataAdapter(select, conn);
DataCommand.Fill(ds);
GridView1.DataSource = ds.Tables[0].DefaultView;
GridView1.DataBind();
GridView1只是:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="True">
<HeaderStyle CssClass="header_grid" />
</asp:GridView>
没有手动声明的列,因为我们不知道用户要浏览多少列。但是,如果他标记了“ImageUrl”列,那么他需要在GridView中查看此图像的缩略图,而不是网络文件路径(\somePc\path\file.jpg)。如果您知道数据源的类型以及包含图像url的列的名称,则可以使用如下事件: 注意:只有在不使用其他模板列的情况下,此操作才有效
gridView1.RowDataBound += new GridViewRowEventHandler(gridView1_RowDataBound);
...
void gridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataView dataSource = (DataView)gridView1.DataSource;
DataColumn imageUrlColumn = dataSource.Table.Columns["ImageUrl"];
if (imageUrlColumn != null)
{
int index = imageUrlColumn.Ordinal;
DataControlFieldCell cell = (DataControlFieldCell)e.Row.Controls[index];
string imageUrl = cell.Text;
cell.Text = GenerateThumbnailUrl(imageUrl);
}
}
}
如果您知道数据源的类型和包含图像url的列的名称,则可以使用如下事件: 注意:只有在不使用其他模板列的情况下,此操作才有效
gridView1.RowDataBound += new GridViewRowEventHandler(gridView1_RowDataBound);
...
void gridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataView dataSource = (DataView)gridView1.DataSource;
DataColumn imageUrlColumn = dataSource.Table.Columns["ImageUrl"];
if (imageUrlColumn != null)
{
int index = imageUrlColumn.Ordinal;
DataControlFieldCell cell = (DataControlFieldCell)e.Row.Controls[index];
string imageUrl = cell.Text;
cell.Text = GenerateThumbnailUrl(imageUrl);
}
}
}
@我在例子上解释得更多了。@just_name我在例子上解释得更多了。。