Adding dropdowns to each row in a column of a dataview - c#

I have a dataview with the following columns:
<Columns>
<asp:BoundField DataField="report_type" HeaderText="Report Type" ReadOnly="True" SortExpression="report_type"/>
<asp:BoundField DataField="progress" HeaderText="Progress" SortExpression="progress"/>
</Columns>
This works fine, it displays records from the database.
How do I replace the progress column and make it contain a dropdown for each row? Where the dropdown contains complete and incomplete?

You can use Template column in order to customize your render
<asp:TemplateField HeaderText="..">
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>

If you want to use dropdownlist in edit mode, then:
<asp:TemplateField HeaderText="..">
<ItemTemplate>
<%# Eval("progress") %>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlProgress" runat="server" DataTextField="progress" />
</EditItemTemplate>
</asp:TemplateField>

Related

How to set gridview template fields in right side?

I have a gridview with template fields.Now all template fields are showing in the left side of gridview. How can I set it to the right side? My gridview is
<asp:GridView ID="GridView1" BorderWidth="1px" CellSpacing="18" CellPadding="10" runat="server">
<Columns >
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="LinkButton4" runat="server">Single OT</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="LinkButton4" runat="server">Double OT</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
And also binding a query with this gridview. My Query is
qry="select OT from OverTime where EmpCode=#EmpCode"
dr=conn.query(qry);
GridView1.DataSource = dr;
GridView1.DataBind();
Youcan use the ItemStyle-HorizontalAlign attribute:
<asp:TemplateField ItemStyle-HorizontalAlign="Right">
For particular Template Column you can add ItemStyle tag like,
<asp:TemplateColumn>
<ItemStyle HorizontalAlign="Right" Width="150px"></ItemStyle>
</asp:TemplateColumn>
so that the content within that particular Template Column gets aligned towards right
Hope this helps!!

GridView adding column with button

I need to add column with button in GridView ? i tied with asp:button i got error also i tried asp:ButtonField i got this error:
"Error Creating Control - narudzbaGridType 'System.Web.UI.WebControls.ButtonField' does not have a public property named 'ID'.
but i gave ID name to my Button field asp:ButtonField ID="example"
<asp:GridView ID="narudzbaGrid" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Let" HeaderText="Let"/>
<%--<asp:BoundField DataField="Kolicina" HeaderText="Kolicina"/>--%>
</Columns>
</asp:GridView>
You can use TemplateField like this (add to columns block):
<asp:templatefield headertext="Author Name">
<itemtemplate>
<asp:button id="buttonl"
Text= 'Click Me'
runat="server"/>
</itemtemplate>
</asp:templatefield>
Hi you need to add an TemplateField. Everybody like use ImageButton but if you want use other control go ahead.
<asp:TemplateField HeaderText="Edit" ItemStyle-HorizontalAlign="Center" >
<ItemTemplate>
<asp:ImageButton ID="imgBtnEditar" runat="server" ImageUrl="~/iconos/Image.png" CommandName="edit" ToolTip="Edit">
</asp:ImageButton>
</ItemTemplate>
<ItemStyle Height="8px"></ItemStyle>
</asp:TemplateField>

Populating a gridview column with an object list

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>

How to define number of rows of GridView at runtime in code behind using C#

I need to develop such a program in which the GridView's rows should be decided at run time.
i.e. I have a table in database called dealer capacity.
Dealer_ID Capacity
D0001 5
Now when the Dealer D00001 is selected from combo box the number of rows in grid view should be 5. I want to use the template field also.
My code for GridView is:
<asp:GridView ID="grdlicence" runat="server" DataKeyNames="Version_id" GridLines="None" BorderStyle="Solid" AutoGenerateColumns="false" AllowSorting="true"
CssClass="mGrid table"
PagerStyle-CssClass="pgr"
AlternatingRowStyle-CssClass="alt" >
<Columns>
<asp:BoundField DataField="Version_name" ItemStyle-CssClass="uppercase" ItemStyle-Width="150px" HeaderText="Version" HeaderStyle-HorizontalAlign="Left" />
<asp:BoundField DataField="Version_id" Visible="false" HeaderText="Version" HeaderStyle-HorizontalAlign="Left" />
<asp:TemplateField HeaderText="Price">
<ItemTemplate>
<center><asp:TextBox ID="txtprice" CssClass="alignments TextStyle" MaxLength="5" runat="server" ></asp:TextBox></center>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Licence Id">
<ItemTemplate>
<center><asp:TextBox ID="txtlicenceid" CssClass="alignments uppercase" runat="server" ></asp:TextBox></center>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Purchase Date">
<ItemTemplate>
<center><asp:TextBox ID="txtpurchasedate" onfocus="showCalendarControl(this);" CssClass="alignments TextStyle" runat="server"></asp:TextBox></center>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Expiry Date">
<ItemTemplate>
<center><asp:TextBox ID="txtexpirydate" onfocus="showCalendarControl(this);" CssClass="alignments TextStyle" runat="server"></asp:TextBox></center>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Upload File">
<ItemTemplate>
<center><asp:FileUpload ID="fileUpload" runat="server" /></center>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
You need to define PageSize for your GridView and remember to set AllowPaging to true for the GridView
GridView.PageSize Property
Gets or sets the number of records to display on a page in a GridView
control.
The default is 10.
You may see this article: GridView Paging Sample in ASP.NET
You can use the linq Take() and pass the number as parameter.
Updated according to the comment, use following code.
grdlicence.DataSourse= ds.Take(5);
grdlicence.DataBind();

How can I have DropDownList in each and every cell of the GridView when the headers of the GridView are Unknown

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>

Categories