Click here to Skip to main content
15,889,200 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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#.
Posted
Updated 6-Apr-11 1:13am
v2

When you define your columns set the DataPropertyName property to the required column from the dataset. MSDN Example[^].
 
Share this answer
 
Comments
Ankur\m/ 6-Apr-11 7:21am    
Correct, 5!
garfield185 6-Apr-11 7:24am    
Thanks a lot. That made it.
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 give AutoGenerateColumns = false.
 
Share this answer
 
Comments
Toniyo Jackson 6-Apr-11 7:21am    
This solution is for win forms
garfield185 6-Apr-11 7:24am    
Thank you very much. That was the thing.
Toniyo Jackson 6-Apr-11 7:26am    
Welcome
try this its an example
and use dataField same as you database table columns

XML
<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>
 
Share this answer
 
Comments
garfield185 6-Apr-11 7:25am    
Sorry, I didnt say it was for winforms... Thanks anyway

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900