简体   繁体   English

GridView列宽更改

[英]GridView Column Width Altering

Can someone please help me with my problem? 有人可以帮我解决我的问题吗?

I have a gridview in a webpage, whose datasource is an oracle database. 我在网页中有一个gridview,其数据源是一个oracle数据库。 I would like to know if it is possible to change the column widths while you are on the web page? 我想知道当您在网页上时是否可以更改列宽?

string select = "SELECT * FROM TABLE"

OracleCommand cmd = new OracleCommand(select, connectionstring);
cmd.Connection.Open();

GridView.DataSource = cmd.ExecuteReader();
GridView.DataBind();

This outputs the table in the gridview. 这将在gridview中输出表格。 Is there a way to change gridview properties while in the browser? 在浏览器中是否可以更改gridview属性? perhaps some javascript? 也许一些JavaScript?

Thank you in advance, 先感谢您,

Use this code in row databound event of the gridview 在gridview的行数据绑定事件中使用此代码

e.Row.Cells[8].Width = new Unit("800px"); e.Row.Cells [8] .Width = new Unit(“ 800px”);

please ensure your gridview size must be greater than the column size. 请确保您的gridview大小必须大于列的大小。

You can specify these attributes through the styles in the columns of the gridview itself. 您可以通过gridview本身列中的样式指定这些属性。 You can set the AutoGenerateColumns attribute to false, and specify the columns you want, and specify the sizes within each column. 您可以将AutoGenerateColumns属性设置为false,并指定所需的列,并指定每列中的大小。 Furthermore, following good web-design practices, you can use css to style each column. 此外,遵循良好的Web设计实践,您可以使用CSS设置每一列的样式。 If you would like to change it on the fly, I would suggest using ItemTemplates, and adding components you can modify in the field. 如果您想即时更改它,我建议您使用ItemTemplates,并添加可以在现场修改的组件。

Ex: 例如:

<asp:GridView runat="server" AutoGenerateColumns="false" ID="Grid" DataSourceID='MyObjectDataSource' CssClass="grid_table">
                <AlternatingRowStyle CssClass="even" />
                <Columns>
                    <asp:TemplateField>
                        <HeaderTemplate>
                            <div id="NameColumn" runat="server">Name Center</div>
                        </HeaderTemplate>
                        <ItemTemplate>
                            <%# Eval("LastName") %>, <%# Eval("FirstName") %>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField>
                        <HeaderTemplate>
                            <div id="AgeColumn" runat="server">Age</div>
                        </HeaderTemplate>
                        <ItemTemplate>
                            <%# Eval("Age") %>
                        </ItemTemplate>
                    </asp:TemplateField>
                <EmptyDataTemplate>There are no Users</EmptyDataTemplate>
            </asp:GridView>

With this code you can access the column widths: 使用此代码,您可以访问列宽:

NameColumn.Width = 100;
AgeColumn.Width = 2;

However, there are many ways you can accomplish this, including getting the columns directly: 但是,有许多方法可以完成此操作,包括直接获取列:

Grid.Columns[0].HeaderStyle.Width = 100;
Grid.Columns[0].ItemStyle.Width = 100;
Grid.Columns[1].HeaderStyle.Width = 2;
Grid.Columns[1].ItemStyle.Width = 2;

Or again using css: 或再次使用CSS:

Grid.Columns[0].HeaderStyle.CssClass = "name_column_header";
Grid.Columns[0].ItemStyle.CssClass = "name_column_data";
Grid.Columns[1].HeaderStyle.CssClass = "age_column_header";
Grid.Columns[1].ItemStyle.Width = "age_column_data";

Anyways, the world's your oyster, these are just some starting points. 无论如何,全世界都是您的牡蛎,这些只是一些起点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM