Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在gridview中仅显示一些单词的数据_C#_Asp.net_Gridview - Fatal编程技术网

C# 在gridview中仅显示一些单词的数据

C# 在gridview中仅显示一些单词的数据,c#,asp.net,gridview,C#,Asp.net,Gridview,我正在使用网格视图显示最近的消息…有使用数据源 <asp:GridView ID="GridView1" runat="server" AllowPaging="True" Width="586px" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" onsele

我正在使用网格视图显示最近的消息…有使用数据源

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" Width="586px" AutoGenerateColumns="False" 
                                        DataSourceID="SqlDataSource1" 
                                        onselectedindexchanged="GridView1_SelectedIndexChanged" 
                                        onrowcommand="GridView1_RowCommand">
                                        <Columns>
                                            <asp:CommandField HeaderText="show" ShowSelectButton="True" />
                                            <asp:BoundField DataField="user_id" HeaderText="user_id" 
                                                SortExpression="user_id" />
                                            <asp:BoundField DataField="user_name" HeaderText="user_name" 
                                                SortExpression="user_name" />
                                            <asp:BoundField DataField="sender_mail" HeaderText="sender_mail" 
                                                SortExpression="sender_mail" />
                                            <asp:BoundField DataField="message" HeaderText="message" ReadOnly="True" 
                                                SortExpression="message" ControlStyle-Width="70px" ControlStyle-Height="25">
                                            <ControlStyle Height="20px" Width="50px" />
                                            <HeaderStyle Height="10px" Width="70px" />
                                            <ItemStyle Height="20px" HorizontalAlign="Left" Width="70px" />
                                            </asp:BoundField>
                                        </Columns>
                                    </asp:GridView>

在我的数据库中,如果消息太长,那么它会显示在一个字段中。。。 示例:-msg是“你好” 它显示完整的消息……但我只显示数据“你好…”
我也尝试设置宽度和高度,但不起作用。

您可以通过向代码中添加方法来实现这一点,该方法从DB或字符串中获取消息,然后根据需要对该字符串进行任何处理(在特定索引处剪切字符串,然后添加…)。然后该方法返回处理后的字符串

public string cutString(string msg)
   {
      int msgLength = 100;
      return msg.Substring(0, msgLength) + "...";
   }

<asp:Label runat="server" Text='<%# cutString(Eval("message").ToString())%>' />
publicstringcutstring(stringmsg)
{
int msgLength=100;
返回消息子字符串(0,msgLength)+“…”;
}

您可以使用模板字段而不是边界字段

    <asp:TemplateField >
    <HeaderTemplate>Message</HeaderTemplate>
    <ItemTemplate>
    <%# Eval("message").ToString().Substring(0,10) %>
    </ItemTemplate>
 <EditItemtemplate>
              <asp:textbox id="Textbox1"
                text='<%#Eval("message")%>'
                width="90"
                runat="server"/>                                      
            </Edititemtemplate>
    </asp:TemplateField>

消息

这里我们将获取消息的子字符串(仅10个字符)。您可以根据需要进行修改。

您应该在select命令过程中进行修改。有很多方法可以从表中获取所需的sunstring。您可以使用“左”、“子字符串”等。。在sql server.tenx中,对于该答案,它工作得非常好,但当我单击“选择”按钮时,文本框中没有显示完整消息……有代码保护的void GridView1_row命令(对象发送方,GridViewCommandEventArgs e){if(e.CommandName==“选择”){Int16 num=Convert.ToInt16(e.CommandArgument);Label8.Text=GridView1.Rows[num]。Cells[1]。Text;Label9.Text=GridView1.Rows[num]。Cells[2]。Text;msgtextbox.Text=GridView1.Rows[num]。Cells[4]。Text;}@user2154272我已经修改了它,使其也包含一个编辑项模板。这应该可以工作。