Dynamic population in gridview - asp.net

I have a grid view, in which I have 1 column as template field, while other 8 columns are dynamically created.
This template field has a checkbox, while trying to retrieve this checkbox in code-behind file, we get it as null.
When columns are not dynamically created, the checkbox is pretty fine and has a value in code behind.
Below is my code:
<asp:GridView ID="gridResultSet" runat="server" AutoGenerateColumns="false" AllowSorting="true"
OnRowCreated="GridResultSet_RowCreated" OnLoad="GridResultSet_Load" CssClass="reportGrid"
CellPadding="4" OnSorting="GridResultSet_Sorting" OnRowDataBound="GridResultSet_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="Reclass">
<ItemTemplate>
<acesec:CheckBox ID="chkReclass" CssClass="CheckBoxListStyle" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<RowStyle CssClass="gridrow" />
<AlternatingRowStyle CssClass="gridrow" />
<HeaderStyle CssClass="gridheader" />
</asp:GridView>
System.Web.UI.WebControls.CheckBox chbReclass = gridResultSet.Rows[i].FindControl("chkReclass") as System.Web.UI.WebControls.CheckBox;
Do I need to check something in order to access a template field while creation of dynamic columns?
Any body faced same kind of situation?
Pointers will be highly appreciated.
Regards

Related

asp.net hyperlinkfield set navigateurl "http=blabla/Id=" value from database

I am new to asp.net. Please help.
I have 2 files. One default.aspx, one default.aspx.cs.
In the default.aspx, I have:
<asp:GridView ID="DataGridView" runat="server" AutoGenerateColumns="false" RowStyle-BackColor="#A1DCF2" HeaderStyle-BackColor="#3366FF" HeaderStyle-ForeColor="White">
<rowstyle backcolor="LightCyan" forecolor="Black" font-italic="true"/>
<alternatingrowstyle backcolor="PaleTurquoise" forecolor="Black" font-italic="true"/>
<Columns>
<asp:BoundField ItemStyle-Width="15%" DataField="ID" HeaderText="ID" />
<asp:hyperlinkfield text="Log" navigateurl="http://somelink&RecordId=" target="_blank" ItemStyle-Width="15%" HeaderText="Log" />
<asp:BoundField ItemStyle-Width="15%" HeaderText="Delayed Delivery" />
</Columns>
</asp:GridView>
My questions:
Column: hyperlink Log:
I want to bind it to a column in database called "Base_Id", but when I use DataField="Base_Id" in the hyperlink definition, it gives error;
I want to append the base_Id to the end of the url so that it gives navigateurl="http://somelink&RecordId=baseId_value" ; I tried TemplateField, but Visual Studio doesnot recognize it.
for the delayed delivery, I need to compare the data and do some math. Is there a way for me to add a function in default.aspx.cs? if there's, could anyone give an example please?
Any ideas are appreciated. Thanks a lot. =)
Convert the column
<asp:hyperlinkfield text="Log" navigateurl="http://somelink&RecordId=" target="_blank" ItemStyle-Width="15%" HeaderText="Log" />
to TemplateColumn and do this
<asp:TemplateField HeaderText="Log" ItemStyle-Width="15%">
<ItemTemplate>
<asp:HyperLink runat="server"
NavigateUrl='<%# GetUrl(Eval("Base_Id"))%>'
text="Log"></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
and in code-behind
protected string GetUrl(object id)
{
return "http://somelink&RecordId=" + id;
}
Hope this helps!
Regards,
Uroš

RadGrid - How do you change HeaderText of Bound field?

I am using a RadGrid and I had the autogenerated set to true. This results in a column header text of the the database field cap_name. In an ASP.NET GridView you would change it like (after setting autogeneratedcolumns to false:
<Columns>
<asp:BoundField DataField="cap_name" HeaderText="Capability"
SortExpression="cap_name" />
</Columns>
I set autogenerated colums to false and tried to use BoundField but the compiler said to use telerik:GridColumn. How do I use this to get a similar result?
Use <rad:GridBoundColumn HeaderText="Capability" UniqueName="ClmCapability" DataField="cap_name" /> .
NOTE : replace <rad: with the prefix you use for your RadGrid control when you register it. You can find it as TagPrefix in :
<%# Register Assembly="RadGrid.Net2" Namespace="Telerik.WebControls" TagPrefix="rad" %>
You can't use asp:boundfield because it is for asp.net gridview, while you are using telerik radgrid
EDIT :
<rad:RadGrid ID="MyRadGrid" runat="server" EnableAJAXLoadingTemplate="true"
AutoGenerateColumns="false" AllowFilteringByColumn="True" AllowPaging="True"
AllowSorting="True" GridLines="None" Width="60%" EnableAJAX="True" OnNeedDataSource="MyRadGrid_NeedDataSource">
<ClientSettings AllowColumnsReorder="True" ReorderColumnsOnClient="True">
</ClientSettings>
<MasterTableView RetrieveAllDataFields="false" AdditionalDataFieldNames="Name">
<PagerStyle AlwaysVisible="false" Mode="NextPrevNumericAndAdvanced" ShowPagerText="true" />
<Columns>
<rad:GridBoundColumn HeaderText="Capability" UniqueName="ClmCapability" DataField="cap_name" />
</Columns>
</MasterTableView>
</rad:RadGrid>

I want to put table in asp.net grid view

this is my grid view code given below>
<asp:GridView ID="gvReport" runat="server" AutoGenerateColumns="False" GridLines="None"
ShowHeader="true" HorizontalAlign="Left" OnRowCommand="gvReport_RowCommand">
<Columns>
<asp:TemplateField HeaderText="User" ItemStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:Image id="img" text=<%#Eval("Coupon_Image")%>/>
</ItemTemplate>
<HeaderStyle Font-Bold="false" HorizontalAlign="Left" />
</asp:TemplateField>
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:Button ID="getCoupon" runat="server" CssClass="btn" Width="145" Height="34"
CommandName="getCoupon" CommandArgument='<%#Eval("Coupon_Id")%>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
In this it showing like this
Image 1 Button
Image 2 Button
actually i want like this type in grid view
Image1 Image2 Image3
Button Button Button
Please help me ....
advance thanks
A gridview won't work for this. Use the Table control for that, and programmably build the rows and columns. The challenge is the gridview isn't meant to render horizontally. You may be able to make it work with a repeater or ListView too, as you can have the ListView or repeater render the columns, and the layout template be the table and header row.

Hyperlink in datagrid view

I want to set hyperlink field in datagrid view. When user clicks on that link, a query string should be generated and user should be directed to another page. So how can I set hyperlink to generate query string?
<asp:GridView ID="Griddata" runat="server" AutoGenerateColumns="False" CellPadding="1"
GridLines="Horizontal" Width="1000px" ShowFooter="True" CssClass="grid" AlternatingRowStyle-CssClass="alt">
<Columns>
<asp:HyperLinkField HeaderText="ID" DataTextField="rec_id" DataNavigateUrlFields="rec_id"
DataNavigateUrlFormatString="followme.aspx?record={0} " />
<asp:BoundField HeaderText="Login" DataField="LoginName"></asp:BoundField>
</Columns>
</asp:GridView>
This is a sample GridView defined in ASP.NET
You need to specify the <asp:Hyperlinkfield> in the column definition.
In that field, you need to specify the DataTextfield (is what will be displayed on screen in that column), your URL (DataNavigateUrlFormatString) and your parameter that you want to use in that URL (DataNavigateUrlFields)
Note: I'm binding to this grid from code-behind, not through a SqlDatAdaptor but the result is the same.
You will get something like this:
you can do like...
<ItemTemplate>
<asp:HyperLink ID="Edit" runat="server" Text="Edit" NavigateUrl='<%# Eval("DataKeyName", "~/View.aspx?Id={0}") %>' />
</ItemTemplate>
<a href='page.aspx?id=<#Eval("ID")>'>click</a>

I cannot make the headers of my gridview click able for sorting

I have a gridview that I populate based on a query to a db. I'm trying to add sorting on my gridview but I cannot make my column headers clickable. I have allow sorting set to true, and I have my OnSorting event set. My columns are of a few different types. I know the code I need to have in my code behind, but I cannot click on the headers for some reason. Any help on what I'm missing would be appreciated.
<asp:GridView ID="Grid1" runat="server"
AutoGenerateColumns="False"
OnSelectedIndexChanging="Selected_Row_Changing"
DataKeyNames="ApplicationId"
AllowPaging="True"
OnPageIndexChanging="Grid1_PageIndexChanging"
AllowSorting="True"
OnSorting="Grid1_Sorting"
OnRowCreated="OnRowCreated"
OnRowCommand="Grid1_RowCommand"
OnRowDataBound="Grid1_RowDataBound">
<Columns>
<asp:templatefield ...>
<itemtemplate>
<asp:linkbutton .../>
</itemtemplate>
</asp:templatefield>
<asp:BoundField ... />
<asp:HyperLinkField ... />
<asp:ButtonField ... />
</Columns>
</asp:GridView>
You don't have set the SortExpression, have you?
For example:
<asp:boundfield datafield="CompanyName"
headertext="CompanyName"
headerstyle-wrap="false"
sortexpression="CompanyName"/>
Make sure you're not setting the Header Template but rather set the HeaderText attribute for the TemplateField

Resources