在从SQL源绑定数据时,我在设置GridView列宽时遇到问题。我已经测试了代码而没有绑定来自SQL源的数据,它运行得很好。我在这里粘贴两个代码(带有&没有SQL源代码),以便您更好地理解问题。 我从#34开始;没有来自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源绑定时无法设置宽度的原因。提前谢谢。
答案 0 :(得分:0)
你可以像这样设置gridview的宽度
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Cells[1].Width = 1;
e.Row.Cells[0].Width = 1;
e.Row.Cells[4].Width = 75;
e.Row.Cells[5].Width = 1;
}
}
通过.aspx页面的另一个选项
<Columns>
<asp:BoundField DataField="" HeaderText="" ItemStyle-Width="5%" ItemStyle-VerticalAlign="Top" />
<asp:BoundField DataField="" HeaderText="" ItemStyle-Width="25%" ItemStyle-VerticalAlign="Top" />
<asp:BoundField DataField="" HeaderText="" ItemStyle-Width="30%"
ItemStyle-VerticalAlign="" />
<asp:BoundField DataField="" HeaderText="" ItemStyle-Width="25%" ItemStyle-VerticalAlign="Top" />
<asp:BoundField DataField="" HeaderText="" ItemStyle-Width="10%"
ItemStyle-VerticalAlign="Top" />
</Columns>