Auto generate serial no in gridview - asp.net

I want to generate serial no in gridview asp.net.
How can I do it?

try this
<asp:TemplateField>
<ItemTemplate>
<%#Container.DataItemIndex+1 %>
</ItemTemplate>
</asp:TemplateField>
Note : DataItemIndex - Gets the index of the DataItem in the underlying DataSet.

<asp:GridView ID="grdCart" runat="server" Width="100%" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="Sr No" HeaderStyle-Width="5%" HeaderStyle-HorizontalAlign="Left">
<ItemTemplate>
<%# Container.DataItemIndex + 1 %>
</ItemTemplate>
<HeaderStyle CssClass="table_04" HorizontalAlign="Left"></HeaderStyle>
<ItemStyle CssClass="table_02" HorizontalAlign="Left"></ItemStyle>
</asp:TemplateField>
</Columns>
</asp:GridView>

<asp:TemplateField HeaderText="S.No" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Label ID="lblSNo" runat="server" Text='<%#Container.DataItemIndex+1 %>' CssClass="form-label"></asp:Label>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Center"></HeaderStyle>
<ItemStyle HorizontalAlign="Center" Width="10px"></ItemStyle>
</asp:TemplateField>

Related

How to get the ID of the column on gridview radiobutton click

I have one radio button in gridview itemplate field. But when I am selecting the radiobutton i am unable to get the id of that row. So that based on that id textbox value will be autopopulated.
enter code here
<asp:GridView ID="gvItem" SkinID="GridView" runat="server" OnRowDataBound="gvItem_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="" Visible="false">
<ItemTemplate>
<%#Container.DataItemIndex %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:RadioButton ID="rbtnQuantity" runat="server" AutoPostBack="true" OnCheckedChanged="rbtnQuantity_CheckedChanged" />
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" Width="20px" />
</asp:TemplateField>
<%--1--%>
<asp:TemplateField HeaderText="ID" SortExpression="ID">
<ItemTemplate>
<asp:Label ID="lblID" runat="server" Text=' <%# Eval("ID")%>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="90px" />
</asp:TemplateField>
<%--2--%>
<asp:TemplateField HeaderText="Name" SortExpression="Name">
<ItemTemplate>
<asp:Label ID="lblItemName" runat="server" Text='<%# Eval("Name")%>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="140px" />
</asp:TemplateField>
</Columns>
</asp:GridView>
You can try on the radiobutton click event to look for sender.parent.findcontrol("lblID").
this would be on the code-behind and for vb.net it would look like this:
Dim rowID as String = CType(CType(sender, RadioButton).Parent.FindControl("lblID"),Label).Text

RegularExpressionValidator for textbox inside GridView not working

I have GridView:
<asp:GridView ID="MyGridView" runat="server" ShowFooter="true"
AutoGenerateColumns="False" Visible="True">
<Columns>
<asp:BoundField DataField="id" ItemStyle-HorizontalAlign="center"/>
<asp:BoundField DataField="fullName" />
<asp:TemplateField HeaderText="situation>">
<ItemTemplate>
<asp:DropDownList ID="dl_situation" runat="server">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="tbNr" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Data">
<ItemTemplate>
<asp:TextBox ID="tbDate" runat="server"></asp:TextBox>
</asp:RequiredFieldValidator>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
It work fine, but when I add RegularrExpressionValidator for textbox with id="tbDate" mygrid does not work
This regular expression must accept datetime in format "dd.MM.yyyy"
<asp:GridView ID="MyGridView" runat="server" ShowFooter="true"
AutoGenerateColumns="False" Visible="True">
<Columns>
<asp:BoundField DataField="id" ItemStyle-HorizontalAlign="center"/>
<asp:BoundField DataField="fullName" />
<asp:TemplateField HeaderText="situation>">
<ItemTemplate>
<asp:DropDownList ID="dl_situation" runat="server">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="tbNr" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Data">
<ItemTemplate>
<asp:TextBox ID="tbDate" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularCert" runat="server"
ControlToValidate="tbDate" ErrorMessage="Incorect Data"
ValidationExpression=
"(0[1-9]|[12][0-9]|3[01])[-.](0[1-9]|1[012])[-.](19|20)[0-9]{2}"
Display="Dynamic"></asp:RegularExpressionValidator>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ErrorMessage="*" ControlToValidate="tbDate">
</asp:RequiredFieldValidator>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
I think you typed "tbData" instead of "tbDate" in the ControlToValidate property
<asp:RegularExpressionValidator ID="RegularCert" runat="server"
ControlToValidate="tbDate" ErrorMessage="Incorect Data" ValidationExpression= "(0[1-9]|[12][0-9]|3[01])[-.](0[1-9]|1[012])[-.](19|20)[0-9]{2}"
Display="Dynamic"></asp:RegularExpressionValidator>
I changed ClientIDMode to Predictable for TextBox, RegularExpressionValidator and RequiredFieldValidator and began to work
change ControlToValidate="tbData" to ControlToValidate="tbDate" for <asp:RegularExpressionValidator
Have you tried placing the <asp:RegularExpressionValidator ...> inside the
<ItemTemplate>
<asp:TextBox ID="tbDate" runat="server"></asp:TextBox>
</ItemTemplate>
also you need to indicate some text to display if it fails the regex validation
actually what Hallie said is more than likely the answer ha

ASP.NET GridView Display TextBox if Row is Empty

My GridView is DataBound to a SQL data connection which should almost always return data. So using an EmptyDataTemplate does not help me because the GridView should never be empty. But I want the first few rows to be editable so the user can add new information to the GridView. So I've crafted my Select statement to always come back with 3 empty rows. I want those rows to contain TextBoxes instead of Labels. But this:
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False" DataSourceID="SqlDataSource1"
BorderColor="White" BorderStyle="Solid"
onselectedindexchanged="GridView1_SelectedIndexChanged" ShowFooter="False"
ViewStateMode="Disabled">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<input type="checkbox" id ="CheckBox1" class="checkbox" />
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Serial" SortExpression="Serial">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Serial") %>'></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Model" SortExpression="Model">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("Model") %>'></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="From Store">
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" CssClass="dropdownlist"
DataSourceID="SqlDataSource2" DataTextField="Store"
SelectedValue='<%# Bind("Store") %>'>
</asp:DropDownList>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="To Store">
<ItemTemplate>
<asp:DropDownList ID="DropDownList2" runat="server" CssClass="dropdownlist"
DataSourceID="SqlDataSource2" DataTextField="Store">
</asp:DropDownList>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
</Columns>
<FooterStyle HorizontalAlign="Center" ForeColor="White" BorderColor="White" BorderStyle="Solid" />
<HeaderStyle ForeColor="White" BorderColor="White" BorderStyle="Solid" />
<RowStyle BorderColor="White" BorderStyle="Solid" ForeColor="White" />
</asp:GridView>
Produces this:
Where the first 3 rows have uneditable Labels rather than TextBoxes. Is what I want to do possible?
In your template fields add <FooterTemplate></FooterTemplate> This makes the footer row of your gridview a place where you can add new rows. Of course you will need to put items inside the <FooterTemplate>, but the work the same as your <ItemTemplates
So to serve my specific purpose, I decided just to create a table separate from this with an empty row with textboxes. Then a button that used JQuery to take the values from that table to append them into the uneditable row in the DataGrid. Removed the header row from the DataGrid so it all looked like the same table.

Require Two Header Row in Grid View

I have a grid view of Cart Items, which was bonded at run-time, Now my requirement for that is i want two header row in Grid as given below in image , How will it be possible ? can anyone help me?
My Design For Grid http://content.screencast.com/users/Pr6546/folders/Default/media/f7da2da4-f80e-4674-b1d4-0ccd872966d2/Capture.PNG
http://www.screencast.com/t/g4HkqlSpx
Below is my grid Source
<asp:GridView ID="gvCheckOutItems" GridLines="None" ShowFooter="true" DataKeyNames="Item_No"
Width="100%" border="0" CellSpacing="0" CellPadding="5" AutoGenerateColumns="false"
CssClass="Checkout-Grid" runat="server" OnRowCreated="gvCheckOutItems_RowCreated">
<Columns>
<asp:TemplateField HeaderStyle-Width="7%">
<ItemTemplate>
<asp:LinkButton ID="lnkBtnRemove" runat="server" OnClick="lnkBtnRemove_Click" Text="Remove"
CssClass="blue-link"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="Item No" HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Left"
DataField="Item_No" HeaderStyle-Width="7%" />
<asp:BoundField HeaderText="Title" HeaderStyle-HorizontalAlign="Left" FooterStyle-HorizontalAlign="Center"
ItemStyle-HorizontalAlign="Left" DataField="Title" HeaderStyle-Width="25%" />
<asp:BoundField HeaderText="Offered By" HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Left"
DataField="Trainer" HeaderStyle-Width="16%" FooterText="<strong>Order Total</strong>" />
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="txtItemFormat" runat="server" Text='<%# Eval("Item_Format") %>'
Visible="false"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="Format" HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Left"
DataField="Format" HeaderStyle-Width="15%" />
<asp:BoundField HeaderText="Duration" ItemStyle-HorizontalAlign="Right" DataField="Duration"
HeaderStyle-Width="7%" />
<asp:TemplateField HeaderStyle-Width="7%" HeaderStyle-HorizontalAlign="Right" ItemStyle-HorizontalAlign="Right"
FooterStyle-HorizontalAlign="Right" HeaderText="Quantity">
<ItemTemplate>
<asp:TextBox ID="txtQuantity" Enabled="false" CssClass="quantity" Text='<%#Eval("Quantity")%>'
onblur="fnquantityEmpty(this)" onkeyup="extractNumber(this,0,false);" onkeypress="return blockNonNumbers(this, event, false, false);"
runat="server" MaxLength="4" AutoPostBack="true" OnTextChanged="Qty_Changed"></asp:TextBox>
</ItemTemplate>
<FooterTemplate>
<strong>
<asp:Label ID="lblFooterQuantity" runat="server"></asp:Label></strong>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Price" ItemStyle-HorizontalAlign="Right" HeaderStyle-HorizontalAlign="Center"
HeaderStyle-Width="7%">
<ItemTemplate>
<asp:Label ID="lblPrice" runat="server" Text='<%#getConvertedPrice(Eval("Price")) %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="txtItemOwnerID" runat="server" Text='<%# Eval("ItemOwnerID") %>'
Visible="false"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Subtotal" ItemStyle-HorizontalAlign="Right" HeaderStyle-HorizontalAlign="Right"
FooterStyle-HorizontalAlign="Right" HeaderStyle-Width="8%">
<ItemTemplate>
<asp:Label ID="lblSubTotal" runat="server" Text='<%#getConvertedPrice(Eval("Subtotal")) %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<strong>
<asp:Label ID="lblFooterTotalPrice" runat="server" Text="" ToolTip="Total"></asp:Label></strong>
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Regards
Have you considered abandoning the gridview and using a repeater control instead? This would give you more flexibility.
Best thing I would suggest is add a row which should be on top of your datasource and for Title & Type/ Format add Offered By & Terms/Schedule as values respectively.
This would generate the desired output for you.
Happy Coding!!!
An option could be to handle the RowDataBound and on the RowType for Header you set your own RenderMethod and write the HTML yourself.
e.Row.SetRenderMethodDelegate(New RenderMethod(AddressOf RenderHeader))
Please check the links below. They provide a solution on how to apply multiple rows in GridView. Hopefully, it will get you going.
Real World GridView: Two Headed & Grouping GridViews
Dynamic Multiple Row Column Grid Header

Type 'System.Web.UI.WebControls.ImageButton' does not have a public property named 'TemplateField'

<asp:GridView ID="grdCatgory" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="Description" HeaderText="Description" />
<asp:TemplateField HeaderText="Edit">
<ItemTemplate>
<asp:ImageButton ID="imgEdit" runat="server" CommandArgument="<%Eval("CategoryID")%>"
CommandName="Edit" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<asp:ImageButton ID="imgDel" runat="server" CommandArgument="<%Eval("CategoryID")%>"
CommandName="Del" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
i am getting following error when build the website...
Type 'System.Web.UI.WebControls.ImageButton' does not have a public property named 'TemplateField'
I think CommandArgument="<%Eval("CategoryID")%>"
need change to
`CommandArgument="<%#Eval("CategoryID")%>"`

Resources