I am working on an asp.net project. How can I add a static column with static text to a GridView which is loaded with a dataset? For example I want a column Check which has values in every row Checked.
<asp:TemplateField HeaderText="YouText">
<ItemTemplate>
<asp:CheckBox runat="server" ID="cb" Checked='<%# DataBinder.Eval(Container, "DataItem.isChecked") %>' />
</ItemTemplate>
</asp:TemplateField>
Or if you just want static text do this:
<asp:TemplateField HeaderText="YouText">
<ItemTemplate>
Your text
</ItemTemplate>
</asp:TemplateField>
<asp:GridView ID="gridView" runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
//static text or checkbox
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Code Behind:
gridView.DataSource = dataset;
gridView.DataBind();
Your static text will show every rows.
So you want the static text with the dynamic bool?
You can use a binding expression.
<ItemTemplate>
Check
<asp:CheckBox ID="myCheckbox" runat="server"
Checked='<%# Eval("IsChecked") %>'
AutoPostBack="true"
OnCheckedChanged="myCheckbox_Checked" />
</ItemTemplate>
Related
<telerik:GridTemplateColumn HeaderText="Code" SortExpression="Code" UniqueName="Code" AllowFiltering="True">
<ItemTemplate>
<asp:LinkButton ID="lbCCode" runat="server" Text='<%# Eval("Code") %>' />
</ItemTemplate>
</telerik:GridTemplateColumn>
So how can I allow RadGrid to filter the results based on the text value of LinkButton? Is there a way to do that?
Please try with the below code snippet.
<telerik:GridTemplateColumn HeaderText="Code" SortExpression="Code" UniqueName="Code" AllowFiltering="True"
DataField="Code">
<ItemTemplate>
<asp:LinkButton ID="lbCCode" runat="server" Text='<%# Eval("Code") %>' />
</ItemTemplate>
</telerik:GridTemplateColumn>
I have added DataField property in template column. if you get any error in grid then please set EnableLinqExpressions="false" in your grid.
I have a gridview of messages that is populated by an object class. There is a list in the object so that for each message, there is a list of recipients. Problem is, right now the gridview is only displaying "Person[]" for each message. I want it to display as a count of the recipients in the list.
Any help would be appreciated.
HTML
<asp:GridView
ID="grdMsgSent"
runat="server"
CssClass="cellSpacing"
OnRowDataBound="grdMsgSent_RowDataBound"
AllowSorting="True"
EmptyDataText="You have not sent any messages."
AllowPaging="True"
PageSize="6"
OnPageIndexChanging="grdMsgSent_PageIndexChanging"
OnSorting="grdMsgSent_Sorting"
AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="Recipients" HeaderText="Recipients" />
</Columns>
</asp:GridView>
C#
grdMsgSent.DataSource = listSentMsg.List;
grdMsgSent.DataBind();
For example, if you have a data column name called UserID, you can populate in grid view using Eval (DataBinding Expression). So your code will be like this in aspx page,
<asp:TemplateField HeaderText="UserID" Visible="false">
<ItemTemplate>
<asp:Label ID="lblUserID" runat="server" Text='<%# Eval("UserID") %>' />
</ItemTemplate>
</asp:TemplateField>
In C# Code Behind,
DataTable dataTable = new DataTable();
DataColumn dataColumn;
dataColumn = new DataColumn("UserID");
You can use a templatefield:
For array[], do this:
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#Eval("Recipients.Length") %>' />
</ItemTemplate>
</asp:TemplateField>
For List, do this:
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#Eval("Recipients.Count") %>' />
</ItemTemplate>
</asp:TemplateField>
This is how i created a grid view in my ASP.NET C# project. This grid has 4 columns. I want to add a 5th column and add a button on each and every row. How can i do this ?
<asp:GridView ID="gv" runat="server" CellPadding="1" Width="900px"/>
Set AutoGenerateColumns="False" in the gridview markup
Define columns as BoundField (or TemplateField if you wish)
Add aTemplateField for the button in the last column
Sum up:
<asp:GridView runat="server" ID="gv" AutoGenerateColumns="False" CellPadding="1" Width="900px">
<Columns>
<%-- <asp:BoundField /> Definitions here --%>
...
<asp:TemplateField>
<ItemTemplate>
<asp:Button Text="Click ME" runat="server" ID="btn" OnClick="Clicked" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
suppose you are binding data table having 4 column say col1,col2,col3 and col4
then your gird view in .aspx will be like;
<asp:GridView runat="server" ID="gv" AutoGenerateColumns="False" CellPadding="1" Width="900px" OnRowCommand="gv_RowCommand">
<Columns>
<asp:TemplateField HeaderText="col1">
<ItemTemplate>
<%#Eval("col1")%>// "col1" is field of your database
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="col2">
<ItemTemplate>
<%#Eval("col2")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="col3">
<ItemTemplate>
<%#Eval("col3")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="col4">
<ItemTemplate>
<%#Eval("col4")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnOK" runat="server" Text="OK" CommandName="show" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
*.cs page will be *
protected void gv_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.ToLower() == "show")
{
//your code on click event
}
}
I have a GridView, and The headers of the GridView changes every time I run my program. I want DropDownList in each and every cell of my GridView as shown in the attached image.
According to the image:
The values in the DropDownList under each header are = {1,2,3,4,5,6,7,8,9,10}.
Suppose I select value 2 from the DropDownList for KITCHEN2, When I hit on SAVE, I want 2 Lamps(Lamp1, Lamp2)(provided I have selected Lamp_profile in the first column of my GridView) to be updated in the database for Kitchen2. Similarly I want this event to happen at once for all the values that I have selected in GridView when I hit SAVE.
Therefore my Gridview is just a way to provide input, not bound to any datasource.
How do I achieve it. Any help would be useful. Thank you.
You can try with TemplateField
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Kitchen1">
<ItemTemplate>
<asp:DropDownList ID="Kitchen1_DropDownList" runat="server"></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Kitchen2">
<ItemTemplate>
<asp:DropDownList ID="Kitchen2_DropDownList" runat="server"></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Kitchen3">
<ItemTemplate>
<asp:DropDownList ID="Kitchen3_DropDownList" runat="server"></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Kitchen4">
<ItemTemplate>
<asp:DropDownList ID="Kitchen4_DropDownList" runat="server"></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
You must know how bind your data in code behind (You can use Eval.DataBinder)
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddl = (DropDownList)e.Row.FindControl("DropDownList1");
DropDownList Kitchen1DropDownList = (DropDownList)e.Row.FindControl("Kitchen1_DropDownList");
....
}
}
You can use TemplateFields for the DropDownLists.
<asp:GridView ID="GridView1" AutoGenerateColumns="false" runat="server" >
<Columns>
<asp:TemplateField HeaderText="Col1">
<ItemTemplate>
<asp:DropDownList ID="Ddl1" runat="server"></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Col2">
<ItemTemplate>
<asp:DropDownList ID="Ddl2" runat="server"></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Col3">
<ItemTemplate>
<asp:DropDownList ID="Ddl3" runat="server"></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
I need some help again.
I have a GridView with CheckBoxes in first column. If a user checks multiple CheckBoxes and clicks a button I need to display selected rows in edit mode. How do I do that? Any tips?
Some sample code, how I imagine it.
Here is my Gridview MyGV:
<asp:GridView ID="MyGV" runat="server" AutoGenerateColumns="False" DataKeyNames="ID">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="ChkBox" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ID" HeaderText="ID" />
<asp:TemplateField HeaderText="Column1">
<EditItemTemplate>
<asp:TextBox ID="tbColumn1" runat="server" Text='<%# Bind("column1") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="labColumn1" runat="server" Text='<%# Bind("column1") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Column2">
<EditItemTemplate>
<asp:TextBox ID="tbColumn2" runat="server" Text='<%# Bind("column2") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="labColumn2" runat="server" Text='<%# Bind("column2") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="btnEdit" runat="server" Text="EDIT" OnClick="btnEdit_Click" />
In code behind I bind that GridView to display data from SQL table (no problem there). Now I need it to enter selected rows into edit mode (ONLY selected rows) when I click the EDIT button.
protected void btnEdit_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in MyGV.Rows)
{
CheckBox checkbox = (CheckBox)row.FindControl("ChkBox");
if (checkbox.Checked)
{
// ENTER EDIT MODE - Help needed here!! :)
}
}
BindGridView();
}
In a GridView only one row can be in true edit mode at the same time. See question below for a possible workaround:
Put multiple rows of a gridview into edit mode