Nicholas Ong Ответов: 2

Как изменить ширину размер gridview в ASP.NET


Как изменить размер столбца в gridview?

Что я уже пробовал:

<asp:GridView ID="gvStudent" runat="server" AutoGenerateColumns="False" ShowFooter="True" DataKeyNames="StudentID"
            ShowHeaderWhenEmpty="True"
            OnRowCommand="gvStudent_RowCommand" OnRowEditing="gvStudent_RowEditing" OnRowCancelingEdit="gvStudent_RowCancelingEdit"
            OnRowUpdating="gvStudent_RowUpdating" OnRowDeleting="gvStudent_RowDeleting"
        BackColor="White" BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px" CellPadding="4">
        <%-- Theme Properties --%>
        <FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
        <HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" />
        <PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />
        <RowStyle ForeColor="#003399" BackColor="White" />
        <SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
        <SortedAscendingCellStyle BackColor="#EDF6F6" />
        <SortedAscendingHeaderStyle BackColor="#0D4AC4" />
        <SortedDescendingCellStyle BackColor="#D6DFDF" />
        <SortedDescendingHeaderStyle BackColor="#002876" />
        <Columns>
            <asp:TemplateField HeaderText="Name">
                <ItemTemplate>
                    <asp:Label Text='<%# Eval("Name")%>' runat="server" />
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="txtName" Text='<%# Eval("Name")%>' runat="server" />
                </EditItemTemplate>
                <FooterTemplate>
                      <asp:TextBox ID="txtNameFooter" runat="server" />
                </FooterTemplate>
            </asp:TemplateField>

            <asp:TemplateField HeaderText="Photo">
                <ItemTemplate>
                    <asp:Label Text='<%# Eval("Photo")%>' runat="server" />
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="txtPhoto" Text='<%# Eval("Photo")%>' runat="server" />
                </EditItemTemplate>
                <FooterTemplate>
                      <asp:TextBox ID="txtPhotoFooter" runat="server" />
                </FooterTemplate>
            </asp:TemplateField>

            <asp:TemplateField HeaderText="Course">
                <ItemTemplate>
                    <asp:Label Text='<%# Eval("Course") %>' runat="server" />
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="txtCourse" Text='<%# Eval("Course") %>' runat="server" />
                </EditItemTemplate>
                <FooterTemplate>
                      <asp:TextBox ID="txtCourseFooter" runat="server" />
                </FooterTemplate>
            </asp:TemplateField>

            <asp:TemplateField HeaderText="EmailAddress">
                <ItemTemplate>
                    <asp:Label Text='<%# Eval("EmailAddr") %>' runat="server" />
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="txtEmail" Text='<%# Eval("EmailAddr") %>' runat="server" />
                </EditItemTemplate>
                <FooterTemplate>
                      <asp:TextBox ID="txtEmailFooter" runat="server" />
                </FooterTemplate>
            </asp:TemplateField>

            <asp:TemplateField HeaderText="Password">
                <ItemTemplate>
                    <asp:Label Text='<%# Eval("Password") %>' runat="server" />
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="txtPassword" Text='<%# Eval("Password") %>' runat="server" />
                </EditItemTemplate>
                <FooterTemplate>
                      <asp:TextBox ID="txtPasswordFooter" runat="server" />
                </FooterTemplate>
            </asp:TemplateField>

            <asp:TemplateField HeaderText="Status">
                <ItemTemplate>
                    <asp:Label Text='<%# Eval("Status") %>' runat="server" />
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="txtStatus" Text='<%# Eval("Status") %>' runat="server" />
                </EditItemTemplate>
                <FooterTemplate>
                      <asp:TextBox ID="txtStatusFooter" runat="server" />
                </FooterTemplate>
            </asp:TemplateField>

            <asp:TemplateField HeaderText="MentorID" > 
                <ItemTemplate>
                    <asp:Label Text='<%# Eval("MentorID") %>' runat="server" />
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="txtMentorID" Text='<%# Eval("MentorID") %>' runat="server"/>
                </EditItemTemplate>
                <FooterTemplate>
                      <asp:TextBox ID="txtMentorIDFooter" runat="server" />
                </FooterTemplate>
            </asp:TemplateField>

            <asp:TemplateField>
                <ItemTemplate>
                    <asp:ImageButton ImageUrl="~/Admin/AdminImage/editicon.png" runat="server" CommandName="Edit" ToolTip="Edit" Width="20px" Height="20px" />
                    <asp:ImageButton ImageUrl="~/Admin/AdminImage/deleteicon.png" runat="server" CommandName="Delete" ToolTip="Delete" Width="20px" Height="20px" />
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:ImageButton ImageUrl="~/Admin/AdminImage/saveicon.png" runat="server" CommandName="Save" ToolTip="Save" Width="20px" Height="20px" />
                    <asp:ImageButton ImageUrl="~/Admin/AdminImage/cancelicon.png" runat="server" CommandName="Cancel" ToolTip="Cancel" Width="20px" Height="20px" />
                </EditItemTemplate>
                <FooterTemplate>
                    <asp:ImageButton ImageUrl="~/Admin/AdminImage/addusericon.png" runat="server" CommandName="AddNew" ToolTip="Add New" Width="20px" Height="20px" />
                </FooterTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
        <br />
        <asp:Label ID="lblSuccessMessage" Text="" runat="server" ForeColor="Orange" />
        <br />
        <asp:Label ID="lblErrorMessage" Text="" runat="server" ForeColor="BlueViolet" />
    </div>
    
</asp:Content>

Vincent Maverick Durano

Измениться на что? Вы не хотите фиксировать ширину или динамику?

2 Ответов

Рейтинг:
1

Vincent Maverick Durano

Если вы не хотите фиксированной ширины, то попробуйте установить ControlStyle-Width и HeaderStyle-Width в вашей колонке ItemTemplate.

Если вы хотите обернуть содержимое текстового поля в столбец ItemTemplate, вы можете попробовать этот хак:

Aspx-файл:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
   <Columns>
            <asp:TemplateField>
                    <ItemTemplate>
                        <asp:TextBox ID="TextBox1" runat="server" TextMode="MultiLine" Text='<%# Bind("ColumnName") %>'></asp:TextBox>
                    </ItemTemplate>
            </asp:TemplateField>
   </Columns>
  </asp:GridView>


С ФОНОВЫМ КОДОМ:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            ((TextBox)e.Row.FindControl("TextBox1")).Attributes.Add("style", "word-break:break-all;word-wrap:break-word");
        }
}

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
       
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            ((TextBox)e.Row.FindControl("TextBox1")).Width = new Unit(50);
        }
    
}