My Datagrid is not binding all columns

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • whidbey
    New Member
    • Feb 2007
    • 7

    My Datagrid is not binding all columns

    Hello frnds,

    I have created a datagrid with the following code......
    Code:
     
    <asp:datagrid id="DataGrid1" BackColor="#ccccff" BorderColor="black" Width="750" Runat="server" AutoGenerateColumns="False" HeaderStyle-BackColor="#aaaadd" Font-Size="8pt" Font-Name="Verdana" CellSpacing="0" CellPadding="3" ShowFooter="True" GridLines="None" OnItemDataBound="Datagrid1_itemdataBound">
    <Columns>
    <asp:TemplateColumn HeaderText="S.No."></asp:TemplateColumn>
    <asp:TemplateColumn HeaderText="Select">
    <ItemTemplate>
    <asp:CheckBox ID="checkbox1" Runat="server" Checked="False"></asp:CheckBox>
    </ItemTemplate>
    </asp:TemplateColumn>
    <asp:BoundColumn HeaderText="BookId" DataField="BookId" Visible="true"></asp:BoundColumn>
    <asp:BoundColumn HeaderText="CatId" DataField="CatId" Visible="true"></asp:BoundColumn>
    <asp:BoundColumn HeaderText="Book Name" DataField="Title"></asp:BoundColumn>
    <asp:BoundColumn HeaderText="Author" DataField="Author"></asp:BoundColumn>
    <asp:BoundColumn HeaderText="Publisher" DataField="Publisher"></asp:BoundColumn>
    <asp:BoundColumn HeaderText="Price" DataField="Price" DataFormatString="{0:c}"></asp:BoundColumn>
    <asp:BoundColumn HeaderText="Quantity" DataField="Quantity"></asp:BoundColumn>
    <asp:BoundColumn HeaderText="Category" DataField="CatName"></asp:BoundColumn>
    </Columns>
    </asp:datagrid>
    The datagrid is bind with the database on button click event......

    Code:
    sub button1_click(sender as object,e as eventargs) 
        dim book as string 
        dim ctr as integer
        dim con as sqlconnection
        'dim cmd as sqlcommand
        dim da as sqldataadapter
        dim ds as dataset
        dim str as string
        book=txt1.text.toupper()
        cat=dropdownlist1.selectedvalue
        
        'response.write(book)
        ctr=integer.parse(cat)
        'response.write(ctr.tostring())
        str="server=WADHWA;user id=sa;pwd=;initial catalog=OnlineShoppingDatabase"
        con=new SqlConnection(str)
        con.open()
        'str="select bookdetails.catid , bookdetails.Title,bookdetails.author,bookdetails.publisher,bookdetails.price,bookdetails.quantity,CategDetails.CatName from bookdetails inner join categDetails  on CategDetails.catid= bookdetails.catid where bookdetails.title='" & txt1.text.toupper & "' and CategDetails.catid=" & dropdownlist1.selectedvalue
        str="select bookdetails.bookId,bookdetails.CatId,bookdetails.Title,bookdetails.author,bookdetails.publisher,bookdetails.price,bookdetails.quantity,CategDetails.CatName from bookdetails inner join categDetails  on CategDetails.catid= bookdetails.catid where bookdetails.title like '%" & txt1.text.toupper & "%' and CategDetails.catid=" & dropdownlist1.selectedvalue
        da=new SqlDataAdapter()
        da.selectcommand=new SqlCommand()
        da.selectcommand.connection=con
        da.selectcommand.commandtext=str
        ds=new DataSet
        da.fill(ds)
        response.write(ds.tables(0).columns.count.tostring())
        if ds.tables(0).rows.count =0 then
        ' datagrid1.datasource=ds
        'datagrid1.databind()
        'response.redirect("webform5.aspx")
        response.write("No Matches found")
        else
        Panel1.visible=true
        datagrid1.datasource=ds
        datagrid1.databind()
        end if
        con.close()
        end sub
    In this code section,I have used a join query.There are two tables
    BookDetails with fields(bookid,c atid,title,auth or,publisher,pr ice,quantity) and CategDetails with fields(catid,ca tname).

    Now when i display the total number of columns bind with datagrid is only 6 but it has to bind 8 columns (bookid,catid,t itle,author,pub lisher,price,qu antity,catname respectively),w ith this following code :-
    Code:
    Response.write(Datagrid1.items.count.tostring)
    Please why its happening.is there any miscoded by me ????
  • enreil
    New Member
    • Jan 2007
    • 86

    #2
    Can you determine by their contents which columns are not getting bound?

    Comment

    Working...