【发布时间】:2014-09-01 11:18:21
【问题描述】:
当数据从 SQL 源绑定时,我在设置 GridView 列宽度时遇到问题。我已经在没有从 SQL 源绑定数据的情况下测试了代码,它运行良好。我在这里粘贴两个(有和没有 SQL 源代码)代码,以便您更好地理解问题。 我将从“没有来自 SQL 源的绑定数据”开始。 GridView 代码(.aspx):
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="true" AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" CellPadding="4" OnRowDataBound="GridView1_RowDataBound">
<PagerSettings Mode="NumericFirstLast" FirstPageText="First" LastPageText="Last" PageButtonCount="10" Position="Bottom" />
<Columns>
<asp:TemplateField HeaderText="File">
<ItemTemplate>
<asp:LinkButton ID="link" runat="server" CommandArgument='<%# Eval("Num1") %>' Text="Test" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
(.cs) 后面的代码是:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Sample_project_to_set_column_width_of_a_gridview
{
public partial class form1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Bind();
}
}
protected void Bind()
{
DataTable dt = new DataTable();
DataColumn dc1 = new DataColumn("Num1", typeof(string));
DataColumn dc2 = new DataColumn("Num2", typeof(string));
DataColumn dc3 = new DataColumn("Num3", typeof(string));
DataColumn dc4 = new DataColumn("Num4", typeof(string));
dt.Columns.Add(dc1);
dt.Columns.Add(dc2);
dt.Columns.Add(dc3);
dt.Columns.Add(dc4);
DataRow dr = dt.NewRow();
dr["Num1"] = "AAA";
dr["Num2"] = "BBBFFFF";
dr["Num3"] = "AAASSSSS";
dr["Num4"] = "BBBFFHHHF";
DataRow dr2 = dt.NewRow();
dr2["Num1"] = "CCC";
dr2["Num2"] = "DDDFFFFF";
dr2["Num3"] = "CCC";
dr2["Num4"] = "DDDFFFFF";
dt.Rows.Add(dr);
dt.Rows.Add(dr2);
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[0].Width = 100;
e.Row.Cells[0].Style["border-right"] = "2px solid #666666";
e.Row.Cells[0].BackColor = System.Drawing.Color.CornflowerBlue;
e.Row.Cells[1].Width = 200;
e.Row.Cells[1].Style["border-right"] = "2px solid #666666";
e.Row.Cells[1].BackColor = System.Drawing.Color.CornflowerBlue;
e.Row.Cells[2].Width = 400;
e.Row.Cells[2].Style["border-right"] = "2px solid #666666";
e.Row.Cells[2].BackColor = System.Drawing.Color.Lime;
e.Row.Cells[3].Width = 600;
e.Row.Cells[3].Style["border-right"] = "2px solid #666666";
e.Row.Cells[3].BackColor = System.Drawing.Color.Red;
e.Row.Cells[4].Width = 800;
e.Row.Cells[4].Style["border-right"] = "2px solid #666666";
e.Row.Cells[4].BackColor = System.Drawing.Color.Yellow;
}
}
}
}
以上代码完美运行。这是a link 以查看所需的输出。 现在,当我通过 SQL 源将数据绑定到 GridView 时,我看不到所需的结果。修改后的代码隐藏文件 (.cs) 代码为:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Sample_project_to_set_column_width_of_a_gridview
{
public partial class form1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Bind();
}
}
protected void Bind()
{
SqlConnection con = new SqlConnection("Data Source=MEHDI-PC\\SQLEXPRESS; Initial Catalog=PIMS; Integrated Security=true;");
{
using (SqlCommand cmd = new SqlCommand())
{
String sql = "select * from dbo.Documents";
cmd.Connection = con;
cmd.CommandText = sql;
con.Open();
DataSet ds = new DataSet();
using (SqlDataAdapter adp = new SqlDataAdapter(cmd))
{
adp.Fill(ds);
}
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
}
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[0].Width = 100;
e.Row.Cells[0].Style["border-right"] = "2px solid #666666";
e.Row.Cells[0].BackColor = System.Drawing.Color.CornflowerBlue;
e.Row.Cells[1].Width = 200;
e.Row.Cells[1].Style["border-right"] = "2px solid #666666";
e.Row.Cells[1].BackColor = System.Drawing.Color.CornflowerBlue;
e.Row.Cells[2].Width = 400;
e.Row.Cells[2].Style["border-right"] = "2px solid #666666";
e.Row.Cells[2].BackColor = System.Drawing.Color.Lime;
e.Row.Cells[3].Width = 600;
e.Row.Cells[3].Style["border-right"] = "2px solid #666666";
e.Row.Cells[3].BackColor = System.Drawing.Color.Red;
e.Row.Cells[4].Width = 800;
e.Row.Cells[4].Style["border-right"] = "2px solid #666666";
e.Row.Cells[4].BackColor = System.Drawing.Color.Yellow;
}
}
}
}
我通过运行此 SQL 绑定数据得到的输出可以访问 here。
谁能注意到我从 SQL 源绑定时无法设置宽度的原因。提前致谢。
【问题讨论】:
-
不显示图片。显示 HTML。 HTML 看起来像你想要的吗?
-
@JotaBe HTML 将列宽显示为正确但未正确放置在浏览器中。
-
所以 HTML 很好,样式也很好,但是你没有得到你想要的。 IE。宽度在 HTML 中设置但不被尊重?
-
@JotaBe 没错。
-
@JotaBe 有什么解决方法吗?
标签: c# sql asp.net sql-server gridview