I have 8 columns populated as checkboxes in a gridview. I need to know how to get the values from them and store them in a database using C#. Kindly help.
aspx code:
<asp:GridView ID="GridView1" runat="server" CssClass="etable" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="rollnumber" HeaderText="Roll Number" />
<asp:BoundField DataField="name" HeaderText="Name" />
<asp:TemplateField ItemStyle-HorizontalAlign="Center" HeaderText="IBM">
<ItemTemplate>
<asp:CheckBox ID="chkibm" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-HorizontalAlign="Center" HeaderText="FOL">
<ItemTemplate>
<asp:CheckBox ID="chkfol" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-HorizontalAlign="Center" HeaderText="IS">
<ItemTemplate>
<asp:CheckBox ID="chkis" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-HorizontalAlign="Center" HeaderText="CP">
<ItemTemplate>
<asp:CheckBox ID="chkcp" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-HorizontalAlign="Center" HeaderText="WIM">
<ItemTemplate>
<asp:CheckBox ID="chkwim" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-HorizontalAlign="Center" HeaderText="STTPO">
<ItemTemplate>
<asp:CheckBox ID="chksttpo" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-HorizontalAlign="Center" HeaderText="TDM">
<ItemTemplate>
<asp:CheckBox ID="chktdm" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-HorizontalAlign="Center" HeaderText="PA">
<ItemTemplate>
<asp:CheckBox ID="chkpa" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-HorizontalAlign="Center" HeaderText="ID Card">
<ItemTemplate>
<asp:CheckBox ID="chkid" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
If the CheckBox Field is in database use this
<asp:CheckBoxField DataField="IBM" HeaderText="IBM"/>
instead of
<asp:TemplateField ItemStyle-HorizontalAlign="Center" HeaderText="IBM">
<ItemTemplate>
<asp:CheckBox ID="chkibm" runat="server" />
</ItemTemplate>
</asp:TemplateField>
Related
I am trying to add a Foot Note at the bottom of the Grid. but from the below code snippet, I am seeing footnote at the bottom but it is coming in the empty columns. I mean to say if I have 4 columns in the Grid, the Foot Note is displaying after the 4th column. this should not happen. Ideally, the footnote will starts from the first column until the end of the grid. (as a row)
Any suggestions, please?
<asp:Table ID="Table1" BorderWidth="0" CellPadding="0" CellSpacing="0" Width="100%" runat="server">
<asp:TableRow>
<asp:TableCell>
<asp:GridView ID="_gridView1" runat="server"
EnableViewState="false" ShowFooter="true" AllowPaging="false" EnableFixedHeader="true" Width="100%"
AutoGenerateColumns="false" >
<Columns>
<asp:TemplateField HeaderStyle-HorizontalAlign="Left" HeaderStyle-Width="12%" ItemStyle-Width="12%">
<ItemStyle HorizontalAlign="Left" VerticalAlign="Top"/>
<ItemTemplate >
<asp:Label ID="_LineName" runat="server" Text='<%# Eval("LineName") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ID="Entity0" HeaderText="Entity0" HeaderStyle-HorizontalAlign="Left" HeaderStyle-Width="8%">
<ItemStyle HorizontalAlign="Left" VerticalAlign="Top" />
<ItemTemplate>
<asp:Label ID="_entityZero" runat="server" Text='<%# Eval("EntityName0") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ID="Entity1" HeaderText="Entity1" HeaderStyle-HorizontalAlign="Left" HeaderStyle-Width="8%">
<ItemStyle HorizontalAlign="Left" VerticalAlign="Top"/>
<ItemTemplate>
<asp:Label ID="_entityOne" runat="server" Text='<%# Eval("EntityName1") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ID="Entity2" HeaderText="Entity2" HeaderStyle-HorizontalAlign="Left" HeaderStyle-Width="8%">
<ItemStyle HorizontalAlign="Left" VerticalAlign="Top"/>
<ItemTemplate>
<asp:Label ID="_entityTwo" runat="server" Text='<%# Eval("EntityName2") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ID="Entity3" HeaderText="Entity3" HeaderStyle-HorizontalAlign="Left" HeaderStyle-Width="8%">
<ItemStyle HorizontalAlign="Left" VerticalAlign="Top"/>
<ItemTemplate>
<asp:Label ID="_entityThree" runat="server" Text='<%# Eval("EntityName3") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ID="Entity4" HeaderText="Entity4" HeaderStyle-HorizontalAlign="Left" HeaderStyle-Width="8%">
<ItemStyle HorizontalAlign="Left" VerticalAlign="Top"/>
<ItemTemplate>
<asp:Label ID="_entityFour" runat="server" Text='<%# Eval("EntityName4") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ID="Entity5" HeaderText="Entity5" HeaderStyle-HorizontalAlign="Left" HeaderStyle-Width="8%">
<ItemStyle HorizontalAlign="Left" VerticalAlign="Top"/>
<ItemTemplate>
<asp:Label ID="_entityFive" runat="server" Text='<%# Eval("EntityName5") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ID="Entity6" HeaderText="Entity6" HeaderStyle-HorizontalAlign="Left" HeaderStyle-Width="8%">
<ItemStyle HorizontalAlign="Left" VerticalAlign="Top"/>
<ItemTemplate>
<asp:Label ID="_entitySix" runat="server" Text='<%# Eval("EntityName6") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ID="Entity7" HeaderText="Entity7" HeaderStyle-HorizontalAlign="Left" HeaderStyle-Width="8%">
<ItemStyle HorizontalAlign="Left" VerticalAlign="Top"/>
<ItemTemplate>
<asp:Label ID="_entitySeven" runat="server" Text='<%# Eval("EntityName7") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ID="Entity8" HeaderText="Entity8" HeaderStyle-HorizontalAlign="Left" HeaderStyle-Width="8%">
<ItemStyle HorizontalAlign="Left" VerticalAlign="Top"/>
<ItemTemplate>
<asp:Label ID="_entityEight" runat="server" Text='<%# Eval("EntityName8") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ID="Entity9" HeaderText="Entity9" HeaderStyle-HorizontalAlign="Left" HeaderStyle-Width="8%">
<ItemStyle HorizontalAlign="Left" VerticalAlign="Top"/>
<ItemTemplate>
<asp:Label ID="_entityNine" runat="server" Text='<%# Eval("EntityName9") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ID="Entity10" HeaderText="Entity10" HeaderStyle-HorizontalAlign="Left" HeaderStyle-Width="8%">
<ItemStyle HorizontalAlign="Left" VerticalAlign="Top"/>
<ItemTemplate>
<asp:Label ID="_entityTen" runat="server" Text='<%# Eval("EntityName10") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ID="footer" HeaderStyle-HorizontalAlign="Left">
<FooterTemplate>
<asp:Label id="lblFooter" runat="server">'Last Review Date' indicates the date on which an Issuer Rating was last formally reviewed within a twelve-month period or when a Credit Rating Action was last published. <br /></asp:Label>
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</asp:TableCell>
</asp:TableRow>
</asp:Table>
I have written below lines of code in order to hide and show column in gridview based on the condition.
<asp:TemplateField HeaderText="FirstName" Visible='<%# Eval("FirstName") != null ? true:false %>'>
<ItemTemplate>
<asp:Label ID="lblFirstName" runat="server" Text='<%#Eval("FirstName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
It's not working!
Update in code as below :
<asp:TemplateField HeaderText="FirstName" Visible='<%= !string.IsNullOrEmpty(Eval("FirstName")) ? "true" : "false" %>'>
<ItemTemplate>
<asp:Label ID="lblFirstName" runat="server" Text='<%#Eval("FirstName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
Edit
<asp:TemplateField HeaderText="FirstName" Visible='<%# !string.IsNullOrEmpty(Eval("FirstName")) ? "true" : "false" %>'>
<ItemTemplate>
<asp:Label ID="lblFirstName" runat="server" Text='<%#Eval("FirstName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
Grid View
<asp:GridView ID="GridDisplay" runat="server" AutoGenerateColumns="False" BorderStyle="Groove"
BorderWidth="1px" BorderColor="ActiveCaptionText" Width="100%" EmptyDataText="No Record Found"
AllowPaging="True" OnPageIndexChanging="GridDisplay_PageIndexChanging" AllowSorting="True"
OnRowEditing="GridDisplay_RowEditing" OnRowDeleting="GridDisplay_RowDeleting"
CssClass="listbooking ListingTable">
<Columns>
<asp:TemplateField HeaderText="Booking Code">
<ItemTemplate>
<%# Eval("id")%>
</ItemTemplate>
<HeaderStyle Width="5%" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Property Code">
<ItemTemplate>
<%# Eval("property_id")%>
</ItemTemplate>
<HeaderStyle Width="5%" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<%# Eval("sales_username")%>
</ItemTemplate>
<HeaderStyle Width="18%" />
</asp:TemplateField>
</Columns>
</asp:GridView>
i am trying to hide 2 columns(name).
During Binding the grid data
this.GridDisplay.Columns[2].Visible = false;
I have a GridView defined like this:
<asp:GridView ID="GridView_Messages" runat="server" AutoGenerateColumns="False" PageSize="5" CellPadding="4" AllowPaging="true" Width=100% AllowSorting="true" OnSorting="GridView_Messages_Sorting"
ForeColor="#333333" GridLines="None" OnRowDataBound="GridView_Messages_OnRowDataBound" OnPageIndexChanging="GridView_Messages_PageIndexChanged" OnRowCommand="GridView_Messages_OnRowCommand" EmptyDataText="<%$ Resources:Localization, NoMsg %>">
<%--CssClass="Grid" AlternatingRowStyle-CssClass="alt" PagerStyle-CssClass="pgr"--%>
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:TemplateField HeaderText="" ItemStyle-Width="75" ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Middle">
<HeaderTemplate>
<asp:CheckBox ID="allchk" runat="server" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="check" Text="" TextAlign="Right" AutoPostBack="false" Checked="false" runat="server" OnCheckedChanged="GridView_Messages_CheckedChanged"/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="MessageID" ItemStyle-Width="150" ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Middle" Visible="false">
<ItemTemplate>
<asp:Label ID="GridView_Messages_lblMessageID" runat="server" Text='<%# Eval("MessageID") %>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="<%$ Resources:Localization, Title2 %>" ItemStyle-Width="150" ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Middle" SortExpression="it.Title">
<ItemTemplate>
<asp:Label ID="GridView_Messages_lblTitle" runat="server" Text='<%# Eval("Title") %>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Description" ItemStyle-Width="150" ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Middle" Visible="false">
<ItemTemplate>
<asp:Label ID="GridView_Messages_lblDescription" runat="server" Text='<%# Eval("Description")%>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="FromUserID" ItemStyle-Width="150" ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Middle" Visible="false">
<ItemTemplate>
<asp:Label ID="GridView_Messages_lblFromUserID" runat="server" Text='<%# Eval("FromUserID")%>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="<%$ Resources:Localization, From %>" ItemStyle-Width="150" ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Middle" SortExpression="it.UserName">
<ItemTemplate>
<asp:Label ID="GridView_Messages_lblFromUserName" runat="server" Text='<%# Eval("UserName")%>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Notes" ItemStyle-Width="150" ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Middle" Visible="false">
<ItemTemplate>
<asp:Label ID="GridView_Messages_lblNotes" runat="server" Text='<%# Eval("Notes") %>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="<%$ Resources:Localization, IsRead %>" Visible="false" ItemStyle-Width="150" ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Middle" SortExpression="it.IsRead">
<ItemTemplate>
<asp:Label ID="GridView_Messages_lblIsRead" runat="server" Text='<%# Eval("IsRead") %>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="<%$ Resources:Localization, TimeSent %>" ItemStyle-Width="150" ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Middle" SortExpression="it.SentDateTime">
<ItemTemplate>
<asp:Label ID="GridView_Messages_lblDateTime" runat="server" Text='<%# Eval("SentDateTime") %>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="Msg_lnkBtnClk" runat="server" CommandName="SingleClick" Text="click"
Visible="true" CssClass="hidden"/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="Msg_lnkBtnDblClk" runat="server" CommandName="DoubleClick" Text="dblClick"
Visible="true" CssClass="hidden"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<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" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#C7B88D" />
</asp:GridView>
I don't want my OnRowCommand to execute for the first column of my GridView. Actually I don't want my event to fire when some of my checkboxes is clicked. How to determine if I have clicked on a checkbox or I have clicked somewhere else in a gridview? In WPF I could use e.OriginalSource property but here this is not possible.
Using JQuery:
If you haven't already added JQuery to your page, add this tag to your page's head section:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
And this is the actual code. Add this after the link to JQuery, in the .aspx page's head section:
<script type="text/javascript">
$(function(){
$('#<%= GridView_Messages.ClientID %> input[type="checkbox"]')
.click(function(event){
event.stopPropagation();
event.preventDefault();
})
});
</script>
I have this ASPX code:
<asp:GridView ID="GV_" runat="server">
<Columns>
<asp:TemplateField HeaderText="Eliminar">
<ItemTemplate>
<asp:Button ID="Eliminar_BTN__" runat="server" Text="" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Editar">
<ItemTemplate>
<asp:Button ID="Editar_BTN__" runat="server" Text="" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="WI">
<ItemTemplate>
<asp:DropDownList ID="WI_DDL__" runat="server"></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ITEM">
<ItemTemplate>
<asp:DropDownList ID="ITEM_DDL__" runat="server"></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="L" />
<asp:BoundField HeaderText="M" />
<asp:BoundField HeaderText="X" />
<asp:BoundField HeaderText="J" />
<asp:BoundField HeaderText="V" />
<asp:BoundField HeaderText="S" />
<asp:BoundField HeaderText="D" />
</Columns>
</asp:GridView>
What I want to do is to have a button that when I click on it, I add a row to this gridview, the problem is that the gridview has 2 templatefield columns and I don't know how to do it.
Can someone throw some light on this?
Thanks!
Rick is correct; you need to add to the data source. This is a similar example - Dropdownlist in Gridview value
i have a asp.net grid
<asp:GridView ID="GrdUsers" runat="server" AutoGenerateColumns="False"
Width="95%" BorderWidth="1px" BorderColor="Black" PagerStyle-CssClass="pgr" GridLines="Horizontal"
CssClass="DataGridStyle" AllowPaging="True" OnRowCommand="GrdUsers_RowCommand"
OnRowDeleting="GrdUsers_RowDeleting" OnRowEditing="GrdUsers_RowEditing" AllowSorting="true" OnSorting="gridView_Sorting"
OnPageIndexChanging="GrdUsers_PageIndexChanging">
<%--<EmptyDataRowStyle--%>
<EmptyDataRowStyle CssClass="pgr" />
<EmptyDataTemplate>
<asp:Label ID="lblNorecId" CssClass="notification error" runat="server" Text="No records were found using your search criteria."
Font-Bold="true" ForeColor="Brown" Font-Names="Arial" Font-Size="Medium"></asp:Label>
</EmptyDataTemplate>
<Columns>
<%-- <asp:BoundField HeaderText="Name" DataField="User_Fullname" />
<asp:BoundField HeaderText="City" DataField="User_city" />
<asp:BoundField HeaderText="State" DataField="User_state" />
<asp:BoundField HeaderText="Zip" DataField="User_zip" />
<asp:BoundField HeaderText="Joining Date" DataField="User_joiningdate" />--%>
<asp:BoundField DataField="catId" HeaderText="Categery Id" Visible="False" />
<asp:BoundField DataField="MasterCName" HeaderText="Master Categery" />
<asp:BoundField DataField="CatName" HeaderText="Sub Categery" />
<asp:BoundField DataField="GearID" HeaderText="GearID" Visible="False" />
<asp:BoundField DataField="GearName" HeaderText="Gear" />
<asp:TemplateField HeaderText="RentHourly" SortExpression="RentHourly">
<ItemTemplate>
<asp:Label ID="Label7" runat="server" Text="$"></asp:Label>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("RentHourly") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Rentdaily" SortExpression="Rentdaily">
<ItemTemplate>
<asp:Label ID="Label8" runat="server" Text="$"></asp:Label>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("Rentdaily") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="RentWeekend" Visible="false">
<ItemTemplate>
<asp:Label ID="Label9" runat="server" Text="$" Visible="false"></asp:Label>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("RentWeekend") %>' Visible="false"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="RentWeekly" Visible="false">
<ItemTemplate>
<asp:Label ID="Label10" runat="server" Text="$" Visible="false"></asp:Label>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("RentWeekly") %>' Visible="false"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="btnEdit" runat="server" CommandName="edit" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "GearID") %>'
ImageUrl="~/img/icons/16/edit_icon.png" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="btnDel" runat="server" CommandName="delete" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "GearID") %>'
ImageUrl="~/img/icons/16/cancel.png" />
<cc1:ConfirmButtonExtender ID="btnDel_ConfirmButtonExtender" runat="server" ConfirmText="Do You Want To Delete?"
Enabled="True" TargetControlID="btnDel">
</cc1:ConfirmButtonExtender>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<PagerSettings Mode="NextPrevious" NextPageText="Next" PreviousPageText="Last" />
<RowStyle HorizontalAlign="Center" />
<AlternatingRowStyle BackColor="#DBDBDB" ForeColor="Black" HorizontalAlign="Center" />
</asp:GridView>
now i want to sort them renthourly and rentdaily in ascending and descending order
Looks like this is an old question, but your best bet is to use an ObjectDataSource to bind your grid.
Once you use the ObjectDataSource, you can have it automatically supply the SortDirection, SortColumn, CurrentPage and PageSize to the SelectMethod you have specified on the ObjectDataSource.
Inside this business method (your SelectMethod) you can cleanly issue the appropriate command to query only the data needed for the current page and apply the appropriate sorting expression.