绑定上的DataGridView AutoGenerateColumns [英] DataGridView AutoGenerateColumns on Binding
问题描述
我正在尝试使用从Select中获取的数据填充DataGridView.
我不想自动创建列,我已经定义了一些带有样式的东西.但是,在定义数据源时,我的DataGridView会显示预期的行数,但为空.
如果将AutoGenerateColumns设置为true,则会获得所有预定义的列(空)以及选择语句中的所有列.
那么,如何将select中返回的每个列与datagridview上的预定义列进行匹配?
谢谢.
VS2005,C#.
Hi, I''m trying to fill a DataGridView with the data taken from a Select.
I don''t want the columns to be created automatically, I already defined some with styles and all that stuff. However, when defining the datasource, my DataGridView shows the expected number of rows, but empty.
If I set AutoGenerateColumns to true, I get all my predefined columns (empty) plus all the columns from the select sentence.
So, how do I match each returned column from the select, with my predefined columns on the datagridview?
Thanks.
VS2005, C#.
推荐答案
定义列时,将DataPropertyName
属性设置为数据集中所需的列. MSDN示例 [ ^ ].
When you define your columns set the DataPropertyName
property to the required column from the dataset. MSDN Example[^].
转到Datagridview属性->列.
然后,您将获得一个小窗口.在右侧,您可以在列属性中看到"DataPropertyName".
为每个列设置数据库列名称.
然后给AutoGenerateColumns = false
.
Goto Datagridview Properties --> Columns.
Then you will get small window. In the right side you can see ''DataPropertyName'' in column properties.
For each column set the database column name.
Then giveAutoGenerateColumns = false
.
尝试一下它的例子
并使用与数据库表列相同的dataField
try this its an example
and use dataField same as you database table columns
<asp:GridView ID="grd_InterviewCategoryList" AllowPaging="true" PageSize="10" DataKeyNames="iID" runat="server" Width="60%"
GridLines="none" AllowSorting="true" AutoGenerateColumns="False" EmptyDataText="No Records Found"
EmptyDataRowStyle-ForeColor="red" EmptyDataRowStyle-HorizontalAlign="Center"
AlternatingRowStyle-BackColor="#F7F7F7" CellPadding="4" CellSpacing="4" OnSelectedIndexChanged="grd_InterviewCategoryList_SelectedIndexChanged"
OnRowDeleting="grd_InterviewCategoryList_RowDeleting" OnPageIndexChanging="grd_InterviewCategoryList_PageIndexChanging"
OnSorting="grd_InterviewCategoryList_Sorting">
<HeaderStyle HorizontalAlign="Center" />
<Columns>
<asp:TemplateField HeaderText="SR.NO.">
<ItemTemplate>
<%# (grd_InterviewCategoryList.PageSize * grd_InterviewCategoryList.PageIndex) + Container.DataItemIndex + 1%>
</ItemTemplate>
<ItemStyle Width="20%" HorizontalAlign="Center" />
</asp:TemplateField>
<asp:BoundField DataField="strCategoryName" HeaderText="Name" SortExpression="strCategoryName">
<ItemStyle Width="30%" HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="Date" HeaderText="Date" SortExpression="Date">
<ItemStyle Width="20%" HorizontalAlign="Center"/>
</asp:BoundField>
</Columns>
</asp:GridView>
这篇关于绑定上的DataGridView AutoGenerateColumns的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!