如何将特定大小放入gridview中的列

时间:2014-01-30 23:38:56

标签: c# asp.net sql gridview

我有这些列的gridview:Comment(255 char),用户名(varchar 50),rating(int 1),date(yyyy-mm-dd)。现在,当这个gridview出现时,它将注释放在一个大单元格中。我希望文本包含在很多行上(比如50个字符)。

以下是我将如何看待gridview:

评论用户名评级日期

aaaaaaaaaaBBBBBBBBBBaaaaaaaaaaBBBBBBBBBBaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbb 2014b-31 bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb cccccccccccccccccccccccccccccccccccccccccccccccccc

以下是

背后的代码
    SqlConnection con = new 
        SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings
        ["ConnectionString"].ConnectionString);

    //======= Insert Query.

    string cmdText = "INSERT INTO Comments VALUES (@comment,@user,@rate, @date)";

    SqlCommand cmd = new SqlCommand(cmdText, con);

    //===== Adding parameters/Values.

    cmd.Parameters.AddWithValue("@comment", txtComment.Text);
    cmd.Parameters.AddWithValue("@user", txtSignature.Text);
    cmd.Parameters.AddWithValue("@rate", lstbxRating.Text);
    cmd.Parameters.AddWithValue("@date", DateTime.Now);

    if (con.State == ConnectionState.Closed)
    {
        con.Open();
    }
    int nbrRecords = cmd.ExecuteNonQuery();

    con.Close();

    if (nbrRecords >= 1)
    {
        GridView1.DataBind();
    }
}

这是.aspx

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        DataSourceID="LinqDataSource1">
    <Columns>
        <asp:BoundField DataField="comment" HeaderText="comment" ReadOnly="True" 
             SortExpression="comment"  />
        <asp:BoundField DataField="user" HeaderText="user" ReadOnly="True" 
             SortExpression="usager" />
        <asp:BoundField DataField="rate" HeaderText="rate" ReadOnly="True" SortExpression="rate"
             />
        <asp:BoundField DataField="date" HeaderText="date" ReadOnly="True" SortExpression="date" 
            />
    </Columns>
</asp:GridView>

2 个答案:

答案 0 :(得分:0)

为您的单元格设置固定大小,然后将白色空间设置为预包装:

.className { 
   white-space: pre-wrap!important;      /* CSS3 */   
   white-space: -moz-pre-wrap!important; /* Firefox */    
   white-space: -pre-wrap!important;     /* Opera <7 */   
   white-space: -o-pre-wrap!important;   /* Opera 7 */    
   word-wrap: break-word!important;      /* IE */
}

(将课程应用于您的单元格)。

答案 1 :(得分:0)

这是我做的......只需添加ItemStyle-Width =“”属性......简单没有?

以下是.aspx

中的代码
      <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
          DataSourceID="LinqDataSource1">
          <Columns>
              <asp:BoundField DataField="comments" HeaderText="comments" ReadOnly="True" 
                   SortExpression="comments" ItemStyle-Width="1500" />
              <asp:BoundField DataField="userr" HeaderText="usager" ReadOnly="True" 
                   SortExpression="usager" />
              <asp:BoundField DataField="rating" HeaderText="rating" ReadOnly="True" 
                   SortExpression="cote" DataFormatString="{0:0/5}" />
              <asp:BoundField DataField="date" HeaderText="date" ReadOnly="True" 
                   SortExpression="date" DataFormatString="{0:MM-dd-yyyy}" />
          </Columns>
      </asp:GridView>