none
Column is missing while binding to asp.net DataGrid RRS feed

  • Question

  • Hi everyone,

    I'm doing my asp.net homework. What I have to do is to read data from a joined table and bind the retrieved data to an asp.net DataGrid.

    I use Linq to SQL to join and read data from database. But when I bind my data to DataGrid, a column is missing .
    I used a loop to check whether I can read the data from missing column or not, and actually, I could.

    Here is my code and database :
    https://www.dropbox.com/s/ga4zbp3f19dbngq/Asp.net.rar?dl=0

    Here is my code to retrieve data from database, and the column Power is missing

    decimal priceStart;
                decimal priceStop;
    
                #region Price
    
                try
                {
                    priceStart = decimal.Parse(txtPriceFrom.Text);
                }
                catch (Exception)
                {
                    priceStart = 0;
                }
    
                try
                {
                    priceStop = decimal.Parse(txtPriceTo.Text);
                }
                catch (Exception)
                {
                    priceStop = 0;
                }
    
                #endregion
                
                // Select all results.
                var filteredResults =   from results in _databaseController.WaterHeaters
                                        join heaterTypes in _databaseController.WaterHeaterTypes
                                        on results.TypeID
                                        equals heaterTypes.TypeID
                                        where results.Price > priceStart
                                        select new {heaterTypes.TypeName, results.Capacity, results.Power, results.Material, results.Price};
                
                
                // Valid price
                if (priceStop > 0 && priceStart < priceStop)
                {
                    filteredResults =   from results
                                        in filteredResults
                                        where results.Price < priceStop
                                        select  results;
                    
                }
    
                
                // Bind result to the DataGrid.
                tblResultDisplay.DataSource = filteredResults;
                tblResultDisplay.DataBind();
    Can anyone help me please ?
    Thank you :)

    Friday, April 3, 2015 6:36 PM

Answers

  • Hello Linh,

    With you provided project link, I downloaded it and tested it, I found the caused reason is that the DataGrid seems to not be able to binding a nullable property automatically. The workaround is to change the Power to be null able, this might not be what you want since it would change the database schema, so you could try to binding the DataGrid by specifying columns as:

    <asp:DataGrid ID="tblResultDisplay"
    
                    runat="server"
    
                    Style="width: 100%"
    
                    AutoGenerateColumns="false"
    
                    HeaderStyle-HorizontalAlign="Center"
    
                    HeaderStyle-VerticalAlign="Middle"
    
                    ItemStyle-HorizontalAlign="Center"
    
                    ItemStyle-VerticalAlign="Middle">
    
                    <Columns>
    
                        <asp:BoundColumn DataField="TypeName" HeaderText="TypeName" />
    
                        <asp:BoundColumn DataField="Capacity" HeaderText="Capacity" />
    
                        <asp:BoundColumn DataField="Power" HeaderText="Power" />
    
                        <asp:BoundColumn DataField="Material" HeaderText="Material" />
    
                        <asp:BoundColumn DataField="Price" HeaderText="Price" />
    
                    </Columns>
    
                </asp:DataGrid>
    

    For the reason why the DataGrid cannot recognize the null able property, I suggest that you could ask it on the asp.net forum:

    http://forums.asp.net/

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, April 6, 2015 3:34 AM
    Moderator

All replies

  • Can anyone help me please ?
    Sunday, April 5, 2015 7:48 AM
  • Hello Linh,

    With you provided project link, I downloaded it and tested it, I found the caused reason is that the DataGrid seems to not be able to binding a nullable property automatically. The workaround is to change the Power to be null able, this might not be what you want since it would change the database schema, so you could try to binding the DataGrid by specifying columns as:

    <asp:DataGrid ID="tblResultDisplay"
    
                    runat="server"
    
                    Style="width: 100%"
    
                    AutoGenerateColumns="false"
    
                    HeaderStyle-HorizontalAlign="Center"
    
                    HeaderStyle-VerticalAlign="Middle"
    
                    ItemStyle-HorizontalAlign="Center"
    
                    ItemStyle-VerticalAlign="Middle">
    
                    <Columns>
    
                        <asp:BoundColumn DataField="TypeName" HeaderText="TypeName" />
    
                        <asp:BoundColumn DataField="Capacity" HeaderText="Capacity" />
    
                        <asp:BoundColumn DataField="Power" HeaderText="Power" />
    
                        <asp:BoundColumn DataField="Material" HeaderText="Material" />
    
                        <asp:BoundColumn DataField="Price" HeaderText="Price" />
    
                    </Columns>
    
                </asp:DataGrid>
    

    For the reason why the DataGrid cannot recognize the null able property, I suggest that you could ask it on the asp.net forum:

    http://forums.asp.net/

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, April 6, 2015 3:34 AM
    Moderator
  • Thank you for replying me :D
    Monday, April 6, 2015 4:41 PM