I have a GridView, which is bound to a database table. This GridView shows the some books (price, author information, etc).
I wants to add a column "Add to Cart" to this GridView at the end of each book. I added one column with text "Add to cart". But, on debug, it shows an error as "Add to cart is not present in db."
So please tell me how to append this column to the GridView.
Markup
<asp:GridView ID="GridView1" runat="server" BorderStyle="Double"
BorderWidth="3px" CellPadding="4" GridLines="Horizontal" Height="260px"
Width="661px">
<FooterStyle BackColor="White" ForeColor="#333333" />
<HeaderStyle BackColor="#336666" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#336666" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="White" ForeColor="#333333" />
<SelectedRowStyle BackColor="#339966" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F7F7F7" />
<SortedAscendingHeaderStyle BackColor="#487575" />
<SortedDescendingCellStyle BackColor="#E5E5E5" />
<SortedDescendingHeaderStyle BackColor="#275353" />
<Columns>
<asp:ButtonField Text="Add To Cart">
<FooterStyle HorizontalAlign="Right" />
<HeaderStyle HorizontalAlign="Right" />
<ItemStyle HorizontalAlign="Left" />
</asp:ButtonField>
</Columns>
</asp:GridView>
Code
public partial class WebForm9 : System.Web.UI.Page<p>
{
buybl obj = new buybl();
protected void Page_Load(object sender, EventArgs e)
{
obj.dept = "IT";
GridView1.DataSource = obj.select();
GridView1.DataBind();
}
}
Without your markup, it's hard to say. But, it sounds like you may have modelled the "Add To Cart" column after your columns that are bound to the database. You probably have your databound columns set up as BoundFields like this:
<Columns>
<asp:BoundField DataField="Title" HeaderText="Book Title" />
<asp:BoundField DataField="Author" HeaderText="Book Author" />
<asp:BoundField DataField="Price" HeaderText="Book Price" />
</Columns>
Your "Add To Cart" column will not be databound. If you just want to add a column that has a static button, you can add a CommandField to your GridView, like this:
<Columns>
<asp:BoundField DataField="Title" HeaderText="Book Title" />
<asp:BoundField DataField="Author" HeaderText="Book Author" />
<asp:BoundField DataField="Price" HeaderText="Book Price" />
<asp:CommandField SelectText="Add To Cart" ShowSelectButton="True" />
</Columns>
Or you can just add AutoGenerateSelectButton="True" to your GridView markup. Then you can handle the adding of items to your cart in the RowCommand or SelectedIndexChanged events that are fired when you click the Select button
you can add column like this in gridview
<asp:TemplateField >
<HeaderTemplate >
<asp:Button ID="ButtonName" runat="server" Text="Add to Cart" > </asp:Button>
</HeaderTemplate>
</asp:TemplateField>
Use the TemplateField column type -- unlimited customization:
http://msdn.microsoft.com/en-us/library/bb288032.aspx
<asp:TemplateField HeaderText="FirstName" SortExpression="FirstName">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<% #Bind("FirstName") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<% #Bind("FirstName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
Related
I am using Visual Studio 2015 and Entity Framework 6. I have a gridview displaying orders from a database. However, I need a user to be able to click a row and be taken to another page for editing that row after a dialog box confirmation.
This is what I have:
<asp:GridView ID="gridOrders" runat="server" Height="184px" Width="1359px" AutoGenerateColumns="false"
AllowSorting="true" >
<HeaderStyle Font-Bold="true" Font-Size="16pt" BackColor="#cc0000" ForeColor="Black" />
<RowStyle Font-Size="12pt" BackColor="#afadad" ForeColor="White"/>
<AlternatingRowStyle BackColor="#afadad" ForeColor="White" />
<Columns>
<asp:CommandField HeaderText="" SelectText="CANCEL ORDER" ShowSelectButton="true" ControlStyle-ForeColor="White" />
<asp:BoundField HeaderText="First Name" DataField="FirstName" SortExpression="FirstName" />
How do I make the row selection to another page happen with a dialog that asks user if they are sure?
Change your aspx page with the below code
<asp:GridView ID="gridOrders" runat="server" Height="184px" Width="1359px" AutoGenerateColumns="False"
AllowSorting="True">
<HeaderStyle Font-Bold="true" Font-Size="16pt" BackColor="#cc0000" ForeColor="Black" />
<RowStyle Font-Size="12pt" BackColor="#afadad" ForeColor="White" />
<AlternatingRowStyle BackColor="#afadad" ForeColor="White" />
<Columns>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="lnkCancelOrder" runat="server" OnClientClick="return confirm('Are you sure to redirect?')" OnClick="lnkCancelOrder_Click" CausesValidation="False" CommandArgument='<%#Eval("OrderID") %>' CommandName="Select" Text="CANCEL ORDER"></asp:LinkButton>
</ItemTemplate>
<ControlStyle ForeColor="White" />
</asp:TemplateField>
<asp:BoundField HeaderText="First Name" DataField="FirstName" SortExpression="FirstName" />
</Columns>
</asp:GridView>
Write the c# code as follows
You can redirect to another page and pass the orderID as QueryString, and retrieve the complete order information by orderID and show that in an edit mode form
protected void lnkCancelOrder_Click(object sender, EventArgs e)
{
LinkButton lnk = sender as LinkButton;
string orderID = lnk.CommandArgument;
Response.Redirect("AnotherPage.aspx?orderId="+orderID);
}
Write under the <asp:TemplateField> of Gridview as
<asp:LinkButton ID="anchrTag" runat="server" PostBackUrl="Your edit page url" OnClientClick="return confirm('Are u sure to leave this page and want to go for edit?');">Edit</asp:LinkButton>
I have what seems on the surface a really simple requirement. I want to update a label with a value from the data source when a user clicks a field in a datagrid control.
My page has an SqlDataSource control on it, which returns (for example) 6 columns. I display 5 of them in the datagrid, and want the sixth column shown in a label when the user selects a row.
I have tried various things, with limited success. One way I was convinced would work was to include the column in the datagrid, but set it to Visible="false". However, it turns out that if you do this, the value of row(5).text is ""... Not what I expected.
Any quick way of achieving this?
-- EDIT - Added in code samples --
<asp:GridView ID="gridL250Tickets" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#999999" BorderStyle="Ridge" BorderWidth="2px" Caption="Last 250 Tickets" CellPadding="3" CellSpacing="1" DataKeyNames="TICKETID" DataSourceID="sqlSlxL250Tickets" AllowPaging="True" AllowSorting="True" HorizontalAlign="Center" Width="75%" PageSize="6">
<Columns>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="SUBJECT" HeaderText="Ticket" SortExpression="SUBJECT" />
<asp:BoundField DataField="RECEIVEDDATE" DataFormatString="{0:d}" HeaderText="Received" SortExpression="RECEIVEDDATE" />
<asp:BoundField DataField="COMPLETEDDATE" DataFormatString="{0:d}" HeaderText="Completed" SortExpression="COMPLETEDDATE" />
<asp:BoundField DataField="AREA" HeaderText="Area" SortExpression="AREA" />
<asp:BoundField DataField="CATEGORY" HeaderText="Category" SortExpression="CATEGORY" />
<asp:BoundField DataField="ISSUE" HeaderText="Issue" SortExpression="ISSUE" />
<asp:BoundField DataField="notes1" HeaderText="Notes" SortExpression="notes1" Visible="False" />
<asp:BoundField DataField="USERNAME" HeaderText="Ass. To" SortExpression="USERNAME" />
<asp:BoundField DataField="notes" HeaderText="Notes - l" SortExpression="notes" Visible="false" />
</Columns>
<FooterStyle BackColor="#C6C3C6" ForeColor="Black" />
<HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#E7E7FF" />
<PagerStyle BackColor="#C6C3C6" ForeColor="Black" HorizontalAlign="Right" />
<RowStyle BackColor="#DEDFDE" ForeColor="Black" />
<SelectedRowStyle BackColor="#9471DE" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#594B9C" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#33276A" />
</asp:GridView>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
VB Code:
Private Sub gridL250Tickets_SelectedIndexChanged(sender As Object, e As EventArgs) Handles gridL250Tickets.SelectedIndexChanged
Dim row As GridViewRow = gridL250Tickets.SelectedRow
Label1.Text = row.Cells(9).Text
End Sub
You could use a hidden <asp:TemplateField> with a label instead. I tested that out and it seems to work.
<asp:GridView ID="gridL250Tickets" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#999999" BorderStyle="Ridge" BorderWidth="2px" Caption="Last 250 Tickets" CellPadding="3" CellSpacing="1" DataKeyNames="TICKETID" DataSourceID="sqlSlxL250Tickets" AllowPaging="True" AllowSorting="True" HorizontalAlign="Center" Width="75%" PageSize="6">
<Columns>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="SUBJECT" HeaderText="Ticket" SortExpression="SUBJECT" />
<asp:BoundField DataField="RECEIVEDDATE" DataFormatString="{0:d}" HeaderText="Received" SortExpression="RECEIVEDDATE" />
<asp:BoundField DataField="COMPLETEDDATE" DataFormatString="{0:d}" HeaderText="Completed" SortExpression="COMPLETEDDATE" />
<asp:BoundField DataField="AREA" HeaderText="Area" SortExpression="AREA" />
<asp:BoundField DataField="CATEGORY" HeaderText="Category" SortExpression="CATEGORY" />
<asp:BoundField DataField="ISSUE" HeaderText="Issue" SortExpression="ISSUE" />
<asp:BoundField DataField="notes1" HeaderText="Notes" SortExpression="notes1" Visible="False" />
<asp:BoundField DataField="USERNAME" HeaderText="Ass. To" SortExpression="USERNAME" />
<%--<asp:BoundField DataField="notes" HeaderText="Notes - l" SortExpression="notes" Visible="false" />--%>
<asp:TemplateField Visible="false">
<ItemTemplate>
<asp:Label ID="lblNotes" runat="server" Text='<%# Eval("notes") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
//...
Then just find the label in your event and use its text.
Private Sub gridL250Tickets_SelectedIndexChanged(sender As Object, e As EventArgs) Handles gridL250Tickets.SelectedIndexChanged
Dim row As GridViewRow = gridL250Tickets.SelectedRow
Dim lblNotes As Label = row.FindControl("lblNotes")
Label1.Text = lblNotes.Text
End Sub
Instead of setting Visible = "False", try setting the actual CSS of the given column. Display = "none", and switch it back when the grid control is clicked.
Something along the lines of lblNotes.Style.Item("display") = "none". You could also do that in the Javascript if you didn't want to have a pageback.
So there's not much to explain, I have a GridView and I put a Delete button which asks if you're sure you want to delete when you click it. I'm using VisualStudio2012 and I've done this in many other pages but I've never gotten this problem.
GridView:
<asp:GridView ID="MaintenanceTable" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="Maintenance_ID" DataSourceID="MaintDataSource" EmptyDataText="There are no data records to display." BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Vertical" Width="1000px">
<AlternatingRowStyle BackColor="#DCDCDC" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton runat="server" ID="DeleteButton" Text="Delete"
CommandName="delete"
OnClientClick="if (!window.confirm('Are you sure you want to delete this item?')) return false;" />
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" ShowSelectButton="True" />
<asp:BoundField DataField="Maintenance_ID" HeaderText="Maintenance_ID" InsertVisible="False" ReadOnly="True" SortExpression="Maintenance_ID" />
<asp:CheckBoxField DataField="Status" HeaderText="Status" SortExpression="Status" />
<asp:BoundField DataField="Date" HeaderText="Date" SortExpression="Date" />
<asp:BoundField DataField="Notes" HeaderText="Notes" SortExpression="Notes" />
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
<HeaderStyle BackColor="#34397D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<RowStyle BackColor="#EEEEEE" ForeColor="Black" />
<SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#0000A9" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#000065" />
</asp:GridView>
This really is the only code I've added in terms of deleting in my GridView:
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton runat="server" ID="DeleteButton" Text="Delete"
CommandName="delete"
OnClientClick="if (!window.confirm('Are you sure you want to delete this item?')) return false;" />
</ItemTemplate>
</asp:TemplateField>
You need to add the following code:
protected void MaintenanceTable_RowCommand(object sender,
GridViewCommandEventArgs e)
{
if (e.CommandName == "Delete")
{
// get the maintenanceId of the clicked row
int maintenanceId = Convert.ToInt32(e.CommandArgument);
// Delete the record
DeleteRecordByID(maintenanceId);
// Implement
}
}
Just incase, make sure you use "Delete" not "delete".
Check this resource GridView Delete, with Confirmation
I am using grid view to show records from database. Gridview has pagination and pagesize is set as 15. And one column has link field when user click this link it proceed some functionality. Now what is my problem is
1) when number of records less than page size the last page is
shrunk. it's k for me. but on clicking view link in grid view the
empty row added automatically. how do i remove these empty row?
2) on clicking view link the page index changed to 1. example i am in
3 page of grid view and clicking view link in 3rd page the page index
number changed to 1 but the page shows 3rd row record. how do i fix
this..?
please any can help me.. thanks in advance
grid view code:-
<asp:GridView ID="gvGRNListAll" runat="server" AutoGenerateColumns="False" Style="width: 100%;"
AllowPaging="True" PageSize="15" OnPageIndexChanging="gvGRNListAll_PageIndexChanging"
CellPadding="4" ForeColor="#333333" BorderColor="#CCCCCC" ShowHeaderWhenEmpty="True"
GridLines="None">
<AlternatingRowStyle BackColor="White" />
<PagerStyle CssClass="pager" />
<Columns>
<asp:TemplateField HeaderText="S.No">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#Container.DataItemIndex+1 %>'></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Left" Width="10%" />
</asp:TemplateField>
<asp:BoundField DataField="LocationName" HeaderText="Location" />
<asp:BoundField DataField="SupplierName" HeaderText="Supplier Name" />
<asp:BoundField DataField="GRNNo" HeaderText="GRN No" />
<asp:BoundField DataField="InvoiceNo" HeaderText="In.No">
<ItemStyle HorizontalAlign="Left" VerticalAlign="Middle" />
</asp:BoundField>
<asp:BoundField DataField="GRNDate" HeaderText="GRNDate" />
<asp:TemplateField HeaderText="Action" SortExpression="ExId" ControlStyle-ForeColor="Blue">
<ItemTemplate>
<asp:HiddenField ID="hdnStoreCode" runat="server" Value='<%# Eval("StoreCode") %>' />
<asp:HiddenField ID="hdnGRNNo" runat="server" Value='<%# Eval("GRNNo") %>' />
<asp:LinkButton ID="lbView" OnClick="lbView_Click" runat="server" Text="View"></asp:LinkButton>
</ItemTemplate>
<ControlStyle ForeColor="#FF3300" />
<HeaderStyle CssClass="GridHeaderROW" Width="10%" />
<ItemStyle CssClass="GridItemROW" Width="10%" />
</asp:TemplateField>
</Columns>
<EmptyDataRowStyle HorizontalAlign="Center" VerticalAlign="Bottom" ForeColor="#FF3300" />
<EmptyDataTemplate>
No Records Found.
</EmptyDataTemplate>
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" HorizontalAlign="Left" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
On PageIndexChanged
protected void gvGRNListAll_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gvGRNListAll.PageIndex = e.NewPageIndex;
SearchData();
gvGRNListAll.PageIndex = 0;
}
Searchdata Code :
public void SearchData()
{
DataTable dtGRN = objBAL.FilterGRNScannedList("filterGRNScannedList", suppliercode, grnno, locationcode, Fromdate, Todate, "");
gvGRNListAll.DataSource = dtGRN;
gvGRNListAll.DataBind();
}
Onclick event Code :
if (sender is LinkButton)
{
GridViewRow gvrCurrent = ((LinkButton)sender).NamingContainer as GridViewRow;
hdnGRNNo = (HiddenField)gvrCurrent.FindControl("hdnGRNNo");
hdnStore = (HiddenField)gvrCurrent.FindControl("hdnStoreCode");
gvGRNListAll.Rows[gvrCurrent.RowIndex].BackColor = System.Drawing.Color.Empty;
gvGRNListAll.Rows[gvrCurrent.RowIndex].BackColor = System.Drawing.ColorTranslator.FromHtml("#D8D8D8");
if (hdnGRNNo != null && hdnStore != null)
{
GetGRNListForNo(hdnGRNNo.Value, Convert.ToInt32(hdnStore.Value));
}
}
on RowDataBound do something like below
private void gvGRNListAll_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if (e.Row.Cells[6].Text == "") // here add the cell no at which the row is coming blank.
e.Row.Visible = false;
}
Hope it helps
I have a command field in a gridview, that as it is, works as it should. When i select it, the row is hilighted. However, i needed to convert this field into a template, so i could give it a ID that i could reference when using a AJAX Mobal control. I did this, and i am able to reference it with my Ajax control just fine, but, now this field does not select the row in the Gridview? The Select value is needed for the Ajax control to pull the data thru. So i think my issue is, How do i use this command field as a template, AND have it select the row? I hope i am explaining my issue correctly.
Here is the snippet of code when i convert it into a template.
Thank you!
<asp:GridView ID="GridView3" runat="server" AllowPaging="True" AutoGenerateColumns="False" CellPadding="3" DataKeyNames="Contact_ID" DataSourceID="SqlDataSource2" ForeColor="#333333" GridLines="None" PageSize="6">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Select" Text="Edit"></asp:LinkButton>
<asp:ModalPopupExtender ID="ModalPopupExtender2" runat="server" TargetControlID="LinkButton1" PopupControlID="DetailsView1" BackgroundCssClass="modalBackground"></asp:ModalPopupExtender>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Contact_ID" HeaderText="Contact_ID" InsertVisible="False" ReadOnly="True" SortExpression="Contact_ID" Visible="False" />
<asp:BoundField DataField="Contact_Assigned_Username" HeaderText="Username" SortExpression="Contact_Assigned_Username" />
<asp:BoundField DataField="Contact_First_Name" HeaderText="First Name" SortExpression="Contact_First_Name" />
<asp:BoundField DataField="Contact_Last_Name" HeaderText="Last Name" SortExpression="Contact_Last_Name" />
<asp:BoundField DataField="Contact_Email_Address" HeaderText="Email Address" SortExpression="Contact_Email_Address" />
<asp:BoundField DataField="Contact_Cell_Phone" HeaderText="Cell Phone" SortExpression="Contact_Cell_Phone" />
<asp:CheckBoxField DataField="Contact_Administrator" HeaderText="Admin" SortExpression="Contact_Administrator" />
<asp:CheckBoxField DataField="Contact_LineStat_Triggers_Email" HeaderText="Email Triggers" SortExpression="Contact_LineStat_Triggers_Email" />
<asp:CheckBoxField DataField="Contact_LineStat_Triggers_Text" HeaderText="Text Triggers" SortExpression="Contact_LineStat_Triggers_Text" />
<asp:CheckBoxField DataField="Contact_Web_Portal" HeaderText="Web Access" SortExpression="Contact_Web_Portal" />
<asp:BoundField DataField="Contact_Customer_ID" HeaderText="Contact_Customer_ID" SortExpression="Contact_Customer_ID" Visible="False" />
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
In the RowDataBound event, try something like this:
protected void GridView3_RowDataBound(object sender, GridViewRowEventArgs e)
{
LinkButton btn = (LinkButton)e.Row.FindControl("LinkButton1");
btn.OnClientClick = Page.ClientScript.GetPostBackEventReference(GridView3, "Select$" + e.Row.RowIndex.ToString()));
}