how to use table adapter to bind with asp gridview? - c#

i am trying to bind my grid to table adapter data, this is what i do :
DataSourceDepartement dpt = new DataSourceDepartement();
DataSourceDepartementTableAdapters.departementTableAdapter
adapter = new DataSourceDepartementTableAdapters.departementTableAdapter();
gridDepartement.DataSource = adapter.GetDataDepartement();
gridDepartement.DataBind();
aspx file :
<asp:GridView ID="gridDepartement" runat="server" CellPadding="4" ForeColor="Black"
GridLines="Horizontal" AutoGenerateColumns="False" BackColor="White"
BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" AllowSorting="true">
<Columns>
<asp:TemplateField ItemStyle-Width="20px">
<ItemTemplate>
<asp:CheckBox Id="cbSelect" runat="server"/>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" ItemStyle-Width="20px" />
<asp:CommandField ShowDeleteButton="True" ItemStyle-Width="20px" />
<asp:TemplateField HeaderText="Departement Code">
<ItemTemplate>
<asp:label runat="server" ID="departementcode" ></asp:label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#CCCC99" ForeColor="Black" />
<HeaderStyle BackColor="#333333" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="White" ForeColor="Black" HorizontalAlign="Right" />
<SelectedRowStyle BackColor="#CC3333" Font-Bold="True" ForeColor="White" />
</asp:GridView>
but somehow, my gridview does not show its data, what i miss?

You need to use the RowDataBound event, access the controls for the row in the GridView and then set the values:
SO post
Also make sure no exceptions are being thrown!

Related

How to set the width of TemplateField column in Grid view?

Hi I am developing a website using c# and asp.net. But my grid view is not showing properly for one page only. Whether I am using the same css class but still the output is something odd.
Here is the output I am getting:
here is my design view code for the grid:
<div style="width: 800px; align-content: center;">
<asp:GridView ID="gvMv" runat="server" AutoGenerateColumns="False" width="400px"
OnRowDataBound="gvMv_RowDataBound" CssClass="Grid" ShowFooter="True">
<FooterStyle Height="25" />
<RowStyle />
<PagerStyle />
<HeaderStyle />
<Columns>
<asp:BoundField DataField="day" HeaderText="Day" HeaderStyle-Width="150" ItemStyle-Height="25" HeaderStyle-Height="30">
<HeaderStyle Height="30px" Width="150px"></HeaderStyle>
<ItemStyle Height="25px"></ItemStyle>
</asp:BoundField>
<asp:TemplateField HeaderText="0.30"></asp:TemplateField>
<asp:TemplateField HeaderText="1.00"></asp:TemplateField>
<asp:TemplateField HeaderText="2.00"></asp:TemplateField>
<asp:TemplateField HeaderText="2.50"></asp:TemplateField>
<asp:TemplateField HeaderText="4.00"></asp:TemplateField>
<asp:TemplateField HeaderText="5.00"></asp:TemplateField>
<asp:TemplateField HeaderText="1.50"></asp:TemplateField>
<asp:TemplateField HeaderText="Total" ItemStyle-ForeColor="#0099FF">
<ItemStyle ForeColor="#0099FF"></ItemStyle>
</asp:TemplateField>
</Columns>
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" Font-Size="Smaller" />
<RowStyle CssClass="rSty" BackColor="#F7F7DE" />
<SelectedRowStyle BackColor="#CE5D5A" Font-Bold="true" ForeColor="White" />
<PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
<HeaderStyle CssClass="hSty" BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
</div>
I want all of the column to be same width. Can anyone please help me on this?
Thank you.
I had the same issue and this is what worked for me
<asp:TemplateField ItemStyle-Width="150px" HeaderText="ABC" ItemStyle-HorizontalAlign="center">
<ItemTemplate>
<asp:Label ID="ABC" runat="server" Text ='<%# Eval("ABC")%>' ></asp:Label>
</ItemTemplate>
<HeaderTemplate>
<asp:TextBox ID="txtBusinessGroup" runat="server" onkeyup="filter_BusinessGroup(this)" CssClass="texbox_header" Width="130px" placeholder="ABC" Text="" ToolTip="TYPE IN THE ABC PLEASE"></asp:TextBox>
</HeaderTemplate>
<ItemStyle HorizontalAlign="Center" Width="150px" />
</asp:TemplateField>
I am showing textfield in the headertext which you can remove.
The width of the fields i change from
<ItemStyle HorizontalAlign="Center" Width="150px" />
or the actual textbox im using or the templatefield
<asp:TemplateField ItemStyle-Width="150px" HeaderText="ABC" ItemStyle-HorizontalAlign="center">
I hope this helps you and works for you or anyone else who might end up here.
thank you
Here is an example how you can do it :
<asp:TemplateField HeaderText="used">
<HeaderStyle Width="100" />
<ItemStyle Width="100" />
</asp:TemplateField>
You can use ItemStyle to set properties for your template field column
Try using this template code:
<asp:TemplateField HeaderText="ABC">
<ItemTemplate>
<asp:Label ID="lblABC" runat="server"
Text='<%#DataBinder.Eval(Container.DataItem, "ABCId") %>'>
</asp:Label>
</ItemTemplate>
<ItemStyle Width="20px" />
<HeaderStyle Width="20px" />
<FooterStyle Width="20px" />
<EditItemTemplate>
<asp:Label ID="lblEditABC" runat="server"
Text='<%#DataBinder.Eval(Container.DataItem, "ABCId") %>'>
</asp:Label>
</EditItemTemplate>
</asp:TemplateField>
i experienced the same. ItemStyle is not working! use HeaderStyle.
GridView1.Columns[2].HeaderStyle.Width = 20;
``If these all above code will note work to increase the width of gridview header then you can try this one also it will work definitely.Simply after YourText putt HTML space tag that is that much how much space you want.
**HeaderText="YourText "**

How to add hyperlink to boundfield in gridview c# asp.net

I have a gridview in aspx page, I need it to go add hyperlink to the Component from BoundField once the user clicks on the Component1 value. How can I add the hyperlink to boundfield that's related to BoundField?
<asp:GridView ID="Module" runat="server" AutoGenerateColumns="False" BackColor="White"
BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4"
DataSourceID="dsrcGetModuleData" Font-Size="0.65em" ForeColor="Black" GridLines="Vertical" DataKeyNames="TestID">
<FooterStyle BackColor="#CCCC99" />
<Columns>
<asp:BoundField DataField="Component1" ItemStyle-Font-Size="Small" HeaderStyle-Width="80px" HeaderStyle-Font-Size ="Medium" SortExpression="Component1" />
</Columns>
<RowStyle BackColor="#F7F7DE" />
<SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
<HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
Try this.
<asp:GridView ID="Module" runat="server" AutoGenerateColumns="False" BackColor="White"
BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4"
DataSourceID="dsrcGetModuleData" Font-Size="0.65em" ForeColor="Black" GridLines="Vertical" DataKeyNames="TestID">
<FooterStyle BackColor="#CCCC99" />
<Columns>
<asp:HyperLinkField DataNavigateUrlFields="StockNumber" HeaderText="Stock Number" DataNavigateUrlFormatString="ReplacePage.aspx?StockNumber={0}" DataTextField="StockNumber" />
</Columns>
<RowStyle BackColor="#F7F7DE" />
<SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
<HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
To pass more than one variable, do this.
<asp:GridView ID="Module" runat="server" AutoGenerateColumns="False" BackColor="White"
BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4"
DataSourceID="dsrcGetModuleData" Font-Size="0.65em" ForeColor="Black" GridLines="Vertical" DataKeyNames="TestID">
<FooterStyle BackColor="#CCCC99" />
<Columns>
<asp:HyperLinkField DataNavigateUrlFields="StockNumber, ID, CITY" HeaderText="Stock Number" DataNavigateUrlFormatString="ReplacePage.aspx?StockNumber={0}&id={1}&CITY{2}" DataTextField="StockNumber" />
</Columns>
<RowStyle BackColor="#F7F7DE" />
<SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
<HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
DataNavigateUrlFields - The fields you would like to pass using the hyperlink column.
DataTextField - Current display field in the DatagridView.
HeaderText - header text which should be the description of the DataTextField value.
You won't be able to have a linkbutton in a bound field. However you can convert it to a TemplateField. Here is an example of my LinkButton.
<asp:TemplateField HeaderText="StockNumber" SortExpression="STOCK NO">
<ItemTemplate>
<asp:LinkButton ID="lbStockNumber" runat="server" Text='<%# Bind("StockNumber") %>' OnClick="lbStockNumber_Click"></asp:LinkButton>
</ItemTemplate>
<HeaderStyle BackColor="Black" ForeColor="White" HorizontalAlign="Left" Width="80px" />
<ItemStyle HorizontalAlign="Left" />
</asp:TemplateField>
To convert it to a TemplateField. Make sure you are in Design View. Then click on the smart tag. Edit Columns, select your column, then below the properties click "Convert to TemplateField"
EDIT: I just noticed you wanted a HyperLink instead of a LinkButton. You will still convert it the same way, but just put a HyperLink instead.
<asp:HyperLink ID="hlStockNumber" runat="server" Text='<%# Bind("StockNumber") %>' OnClick="lbStockNumber_Click"></asp:HyperLink>
Hope this helps!

ASP.NET Grid View row with style never defined

I have a strange problem.
I created a GridView that is bounded to an ObjectDataSource.
The strange thing I have now, that all Grid Rows have the style "background-color: White".
But I never declared this and I can't find any place in my project where it is designed. Neither inline the .aspx nor any css or in any code behind. Simply this style definition doesn't exist in my project.
The information I add to the GridView directly are overwritten by the style definition. Here's what Firebug gets:
<div>
<table id="MainContent_UserGrid" cellspacing="0" cellpadding="4" style="width:100%;border-collapse:collapse;">
<tbody>
<tr style="color:White;background-color:#5D7B9D;font-weight:bold;">
<tr style="color:#333333;background-color:White;">
<td>
<td>name.ofme#mydomain.com</td>
<td>Name of me</td>
<td>Y</td>
<td>
</tr>
<tr style="color:#284775;background-color:White;">
<tr style="color:#333333;background-color:White;">
<tr style="color:#284775;background-color:White;">
<tr style="color:#333333;background-color:White;">
</tbody>
</table>
</div>
Here's my ASPX code of the Grid:
<asp:GridView ID="UserGrid"
runat="server"
AutoGenerateColumns="False"
DataKeyNames="AD_ID"
DataSourceID="UserByAdminAdapter" CellPadding="4"
GridLines="None" Width="100%" AllowPaging="True">
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True"
ShowSelectButton="True" />
<asp:BoundField DataField="AD_ID" HeaderText="AD_ID" ReadOnly="True"
SortExpression="AD_ID"></asp:BoundField>
<asp:BoundField DataField="NAME" HeaderText="NAME" SortExpression="NAME"
ReadOnly="True">
</asp:BoundField>
<asp:BoundField DataField="ACCOUNT_TYPE" HeaderText="ACCOUNT_TYPE"
SortExpression="ACCOUNT_TYPE" Visible="False" ReadOnly="True"></asp:BoundField>
<asp:BoundField DataField="ADMIN_ID" HeaderText="ADMIN_ID"
SortExpression="ADMIN_ID" Visible="False" ReadOnly="True"></asp:BoundField>
<asp:BoundField DataField="BOOKINGS_CONSISTENT" HeaderText="BOOKINGS_CONSISTENT"
SortExpression="BOOKINGS_CONSISTENT" ReadOnly="True"></asp:BoundField>
<asp:BoundField DataField="COMPANY" HeaderText="COMPANY"
SortExpression="COMPANY" Visible="False" />
<asp:TemplateField HeaderText="COMPANY_NAME" SortExpression="COMPANY_NAME">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="Companies"
DataTextField="NAME" DataValueField="ROW_ID"
SelectedValue='<%# Bind("COMPANY") %>'>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("COMPANY_NAME") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<RowStyle BackColor="#000000" ForeColor="#333333" />
<AlternatingRowStyle BackColor="#99FFCC" ForeColor="#284775" />
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<sortedascendingcellstyle backcolor="#E9E7E2" />
<sortedascendingheaderstyle backcolor="#506C8C" />
<sorteddescendingcellstyle backcolor="#FFFDF8" />
<sorteddescendingheaderstyle backcolor="#6F8DAE" />
</asp:GridView>
By the way, changing the background color of a row in the c# code doesn't change anything, also.
I have a workaround through css but that's not "nice".
Thank you right now!
Do you have a theme somewhere in your project that could be overwriting your grid styles?
Could you be modifying the style somewhere in your code behind (I doubt it).
Instead of putting specific styles in your gridview footerStyle, why not make a class in your css and link to that so
<RowStyle BackColor="#000000" ForeColor="#333333" />
becomes
<RowStyle CssClass="ItemStyle" />
It might make it easier to trouble shoot in Firebug and reduce the amount of inline styles (and be more maintainable at the same time).
It's auto styling. You can creat CSS Classes for your GridView and apply them to the following attributes:
<HeaderStyle CssClass="tableHead" />
<RowStyle CssClass="tableRow" />
<PagerStyle CssClass="pager" />
//Example
<asp:GridView runat="server" ID="grdAssetSearchResults" AutoGenerateColumns="false" GridLines="Both" AllowSorting="true" UseAccessibleHeader="true" >
<HeaderStyle CssClass="tableHead" />
<RowStyle CssClass="tableRow" />
<PagerStyle CssClass="pager" />
<Columns>
//Columns
</Columns>
</asp:GridView>

How I can display Image on mouseover event on asp:linkButton

I am using asp.net framework 3.5 and c# 2.0. I am able to bind data in to gridview.
Actually I want to display Image on mouseover event on asp:linkButton, which is used in grid view
<asp:GridView ID="gvTemplate" runat="server" AllowPaging="True" AutoGenerateColumns="False"
CellPadding="4" ForeColor="#333333" GridLines="None" ShowFooter="True" Width="100%"
OnRowCommand="gvTemplate_RowCommand" OnRowDataBound="RowDataBound" OnPageIndexChanging="gvTemplate_PageIndexChanging">
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<Columns>
<asp:TemplateField HeaderStyle-HorizontalAlign="Left" HeaderText="Sr. No">
<ItemTemplate>
<%# Container.DataItemIndex + 1 %>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Left" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Template Name" ItemStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:LinkButton ID="lBtTempID" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"tempName") %>'
CommandName='<%# DataBinder.Eval(Container.DataItem,"tempID") %>' CommandArgument="tempID" onmouseover="javascript:showImage(this.id)"/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderStyle-HorizontalAlign="Left" HeaderText="Template Type">
<ItemTemplate>
<asp:Label ID="lbltempType" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"tempType") %>' />
</ItemTemplate>
<HeaderStyle HorizontalAlign="Left" />
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#999999" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>
If you can use jquery it would be like this
$('.lBtTempClass').mouseover(function() {
/// Do your code
});
you should add a css class to link button
<asp:LinkButton ID="lBtTempID" CssClass="lBtTempClass" runat="server" >

Add Image Column to a GridView After Binding

This is May Sample Code in ASP.NET How Can I Add A similar Image Column To All Rows After Binding
gridview1.DataSource = MB.GetTest();
gridview1.DataBind();
And My ASPX Page:
<asp:GridView ID="gridview1" runat="server">
</asp:GridView>
I really need irt, thanks.
Update
<asp:GridView ID="gridview1" runat="server">
<Columns>
<asp:ImageField>
</asp:ImageField>
</Columns>
</asp:GridView>
hi i would like to explain you a little there is a property called AutoGenerateColumns="false" you need to set this as false
and generate columns in your aspx and bind them Refer this Site
<asp:GridView ID="GridView1" Runat="server"
DataSource='<%# GetData() %>' AutoGenerateColumns="False"
BorderWidth="1px" BackColor="White" CellPadding="3" BorderStyle="None"
BorderColor="#CCCCCC" Font-Names="Arial">
<FooterStyle ForeColor="#000066" BackColor="White"></FooterStyle>
<PagerStyle ForeColor="#000066" HorizontalAlign="Left"
BackColor="White"></PagerStyle>
<HeaderStyle ForeColor="White" Font-Bold="True"
BackColor="#006699"></HeaderStyle>
<Columns>
<asp:BoundField HeaderText="Picutre ID" DataField="PictureID">
<ItemStyle HorizontalAlign="Center"
VerticalAlign="Middle"></ItemStyle>
</asp:BoundField>
<asp:BoundField HeaderText="Title" DataField="Title"></asp:BoundField>
<asp:BoundField HeaderText="Date Added" DataField="DateAdded"
DataFormatString="{0:d}">
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:BoundField>
<asp:ImageField DataImageUrlField="PictureURL"></asp:ImageField>
</Columns>
<SelectedRowStyle ForeColor="White" Font-Bold="True"
BackColor="#669999"></SelectedRowStyle>
<RowStyle ForeColor="#000066"></RowStyle>
</asp:GridView>
There you go with Image column above
<asp:ImageField DataImageUrlField="PictureURL"></asp:ImageField>
MSDN is another best site to refer...
above code is taken from MSDN website.

Categories

Resources