Add a column to a datagrid - asp.net

I am trying to add a column with dropdownlist to a DataGrid
Here is the code for the DataGrid binding datasource
List<CPDEmployee> employeelist = (List<CPDEmployee>)Cache["EmployeeList"];
unverifiedlist.DataSource = employeelist;
unverifiedlist.DataBind();
unverifiedlist.AllowPaging = true;
unverifiedlist.PageSize = 10;
In the page.aspx code is like this
<asp:DataGrid ID="unverifiedlist" runat="server" AllowPaging="true" PageSize="10" OnPageIndexChanged="unverifiedlist_PageIndexChanged">
<Columns>
<asp:BoundColumn HeaderText="Surname" DataField="Surname" ReadOnly="true">
</asp:BoundColumn>
</Columns>
<Columns>
<asp:TemplateColumn HeaderText="Options" >
<ItemTemplate>
<asp:DropDownList ID="options" runat="server">
<asp:ListItem Value="1">Verified</asp:ListItem>
<asp:ListItem Value="0">Rejected</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText ="Reason">
<ItemTemplate>
<asp:TextBox ID="reason" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
The result is there are two "Surname" fields in the datagrid. The reason I added "Surname" from page side is that i can't set the order of the columns (the dropdownlist should be at last).
Does anyone know how to solve this problem? thanks very much

You have to turn off AutoGenerateColumns and specify all the bound fields.

Related

Is it possible to change the input type of a parameter inside a SqlDataSource?

I created a table using a Sqldatasource in .net webforms. I added a "commandField" that allows you to edit and update the content of this table, but by default the input type is "text". I would like to have different input types for some columns. For example the description column should be a "textarea" and the category column should be a "select dropdown".
I tried editing the input type= text in CSS, but this was a failure. Apparently only the textarea input type allows multiple lines to be displayed for the user.
This is how I'm calling the edit column in the table:
<asp:CommandField ButtonType="Button" ControlStyle-BackColor="DarkOrange" ControlStyle-CssClass="trCBPad" ItemStyle-CssClass="flex-container2" HeaderText="Edit" ShowEditButton="True">
<ControlStyle BackColor="DarkOrange" CssClass="trCBPad"></ControlStyle>
This is the dataField I would like to edit, so that the input is a textarea:
<asp:BoundField DataField="ProofPointId" HeaderText="ProofPointId" InsertVisible="False" ReadOnly="True" SortExpression="ProofPointId" />
If yor are using a gridview
<asp:GridView ID="GridView1" DataSourceId="MyDataSource" DataKeyNames="Code"
AutoGenerateColumns="false" AutoGenerateEditButton="true"
AutoGenerateDeleteButton="true" runat="server">
<Columns>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<%#Eval("Name")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtName" Text='<%# Bind("Name")%>' runat="server" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Description">
<ItemTemplate>
<%#Eval("Description")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox TextMode="Multiline" ID="txtDesctiption"Text='<%# Bind("Description")%>'
runat="server" />
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

GridView - Third row is not selectable

I have a Gridview control where you can only select the first two rows even though there are three rows in the data source.
<asp:GridView CssClass="gridview" ID="gvBucket" runat="server" DataKeyNames="ID" PageSize="13" OnPageIndexChanging="gvBucket_PageIndexChanging" ShowHeader="False"
AutoGenerateColumns="false" AllowPaging="true">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblID" runat="server" Visible="false" Text='<%#Eval("ID") %>' />
<asp:LinkButton ID="lnkBtnShowContentDetail" runat="server" Text='<%#Eval("Name") %>' CommandName="Select" OnClick="lnkBtnName_Click" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<PagerStyle CssClass="gridview-pager"></PagerStyle>
<SelectedRowStyle CssClass="gvSelectedRow" />
</asp:GridView>
The code is below...
List<Entity> data = _manager.GetAllBuckets("All");
if (data != null)
{
gvBucket.DataSource = data;
gvBucket.DataBind();
3 entities are returned from reading the database
Why is the third row not selectable?
Thanks
Make sure that the 3-rd row (all the rows) has the unique value in the "ID" field (<asp:GridView ... DataKeyNames="ID">).

How Change in CodeBehind HeaderImage in GridView

How to dynamically change the image that is in the header?
The idea is to do it when the grid is created in the "ItemDataBound" event
This is my Grid
<asp:DataGrid ID="dgMain" runat="server" GridLines="Horizontal" AutoGenerateColumns="False" AllowPaging="false">
<Columns>
<asp:BoundColumn DataField="PId" Visible="false" />
<asp:TemplateColumn HeaderStyle-Width="80px" HeaderImageUrl="ico_estadistica_despacho.png">
<ItemTemplate>
<asp:ImageButton ID="btnEst" runat="server" BorderWidth="0" ImageUrl="punto.png" CommandName="EstDesp"/>
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="CotId" Visible="false" />
</Columns>
</asp:DataGrid>
I tried something, but did not work out.
If e.Item.ItemType = ListItemType.Header Then
dgMain2.Columns.Item(1).HeaderImageUrl = "/Images/ico_estadistica_despacho_redi.png"
End If

Multiple DataNavigateUrlFields... one from datasource, one from drpdownlist

Lets say I have a drop down list and a grid view on a page like this
<asp:GridView ID="gvCategories" runat="server" >
<Columns>
<asp:HyperLinkField DataTextField="CategoryName" DataNavigateUrlFields="CategoryID" DataNavigateUrlFormatString="~/Learning.aspx?categoryID={0" />
</Columns>
</asp:GridView>
I'd like to add this to the URL:
&view=<%=SelectedDropdownlistvalue%>
How can I do this?
I would use TemplateField as:
<asp:GridView ID="gvCategories" runat="server" >
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Eval("CategoryName", "~/Learning.aspx?categoryID={0}")+" &view=" + DropDownList1.SelectedValue %>'
Text="Goto Page"></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Remember the DropDownList.SelectedValue is obtained at postBack, so the link is constructed at that time. To change the link dynamically when the value of the dropdownlist changes set the AutoPostBack="true" of the dropdownlist.

Simple databinding to gridview columns

I have a GridView that I use to show my users the result of a search. I want to allow them to choose which columns are shown on the GridView when performing their search. Simple enough, yes? I wanted to try doing this using just databinding, no events. Unfortunately, my code fails to update the GridView using checkboxes bound to the column's Visible property. The state of the chechboxes changes, but the Visible property of the columns does not.
Snippet of Search.aspx:
<myControl:FacultyGridView ID="FacultyGridView1" runat="server" />
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" Text='<%# Eval("HeaderText") %>' Checked='<%# Bind("Visible") %>' AutoPostBack=true/></ItemTemplate>
</asp:Repeater>
Code-behind snippet in Search.aspx.cs:
protected void Page_Load(object sender, EventArgs e)
{
Repeater1.DataSource = FacultyGridView1.GridView.Columns;
Repeater1.DataBind();
}
To be clear, the GridView is exposed as a public property of a user control named FacultyGridView. Relevant snippet of FacultyGridView.ascx:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
AllowPaging="True" AllowSorting="True" PageSize="25">
<PagerSettings Mode="NumericFirstLast" Position="TopAndBottom" />
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" ReadOnly="True" SortExpression="Name" />
<asp:TemplateField HeaderText="University" SortExpression="UniversityID">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("University.Name") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Division">
<ItemTemplate>
<asp:Repeater ID="Repeater1" runat="server" DataSource='<%# Eval("DivisionMemberships") %>'>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("Division.Name") %>'></asp:Label>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Title" HeaderText="Title" ReadOnly="True" SortExpression="Title" />
<asp:TemplateField HeaderText="Research Type">
<ItemTemplate>
<asp:Repeater ID="Repeater1" runat="server" DataSource='<%# Eval("ResearchTypeMappings") %>'>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("ResearchType.Name") %>'></asp:Label>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Expertise" HeaderText="Expertise" ReadOnly="True" SortExpression="Expertise" />
<asp:HyperLinkField DataNavigateUrlFields="Website" DataTextField="Website" HeaderText="Website"
SortExpression="Website" />
<asp:BoundField DataField="Phone" HeaderText="Phone" ReadOnly="True" SortExpression="Phone" />
<asp:TemplateField HeaderText="Email Address" SortExpression="EmailAddress">
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Eval("EmailAddress", "mailto:{0}") %>'
Text='<%# Eval("EmailAddress") %>'></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Finally, I should mention that the GridView is bound by a Button on the page, but I am not getting updates to the Visible property whether I play with the checkboxes before or after databinding. Furthermore, I have not seen my desired behavior when binding the repeater only on the first Page_Load() using if(!IsPostBack), nor by not using Checkbox.AutoPostback true or false. Any clue as to what I'm doing wrong? I expect it to be something simple, but I'm a bit green here.
As a note: I know how to do this easily with events, but I want to do it with databinding as a learning exercise.
Probably because every time the grid is bound to the data, the column & settings are recreated (with-out your changes).

Resources