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">).
Related
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>
I want to display two column value in one DetailsView cell.
Aspx:
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" DataKeyNames="Id" DataSourceID="data">
<Fields>
<asp:BoundField DataField="TicketNo2" HeaderText="Ticket No" SortExpression="TicketNo2" />
<asp:BoundField DataField="TicketNo" HeaderText="Ticket Serial" SortExpression="TicketNo" />
</Fields>
</asp:DetailsView>
The above code is showing values in two seperate columns of the DetailsView.
How can I display both values in one column?
I find out the answer to display two column value in one details view cell. Given below code worked for me.
ASPX:
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" DataKeyNames="Id" DataSourceID="data">
<Fields>
<asp:TemplateField HeaderText="Ticket No" SortExpression="TicketNo2">
<EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label6" runat="server" Text='<%# Bind("TicketNo2") %>'></asp:Label>
<asp:Label ID="Label7" runat="server" Text='<%# Bind("TicketNo") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Fields>
</asp:DetailsView>
Basically, what I want to do is to bind a Linq query to the datasource of a gridview. Here is my code so far :
ddgDossiers.DataSource = (From c In dbConnection.Campaigns.AsQueryable Select c).ToList
ddgDossiers.DataBind()
It compiles and doesn't complain of anything.
<asp:GridView ID="ddgDossiers" runat="server" CellPadding="0" CellSpacing="0" AllowSorting="True"
AutoGenerateColumns="False" EnableViewState="True" AllowPaging="True" PageSize="35"
PagerStyle-HorizontalAlign="Right" PagerStyle-Mode="NumericPages" PagerStyle-Position="TopAndBottom"
BorderWidth="1" GridLines="Both" BorderColor="#000000" CssClass="mGrid" PagerStyle-CssClass="pgr">
<PagerSettings Position="TopAndBottom" />
<Columns>
<asp:TemplateField Visible="false" HeaderText="UniqueID" ItemStyle-HorizontalAlign="left"
HeaderStyle-HorizontalAlign="Center" ItemStyle-Width="1%" HeaderStyle-Width="1%"
HeaderStyle-CssClass="GridTitle" ItemStyle-CssClass="rowReport" SortExpression="UniqueId">
<ItemTemplate>
<asp:Label ID="lblUniqueID" runat="server" Text='<%#Container.DataItem("Campaign.CampaignId").ToString%>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
The error is pointing on this line :
<asp:Label ID="lblUniqueID" runat="server" Text='<%#Container.DataItem("CampaignId").ToString%>' />
And saying : No default member found for type 'Campaign'.
Is there anything to do with that? Thanks in advance.
change line
Text='<%#Container.DataItem("Campaign.CampaignId").ToString%>'
To
Text='<%# DataBinder.Eval(Container.DataItem, "CampaignId") %>'
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.
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.