ASP.NET Hide Gridview row with Checkbox - asp.net

i am trying to create a dynamic Gridview on a ASP.NET page and i have multiple rows, and added a CheckBox Column.
<body>
<h1>Alerts</h1>
<form id="form1" runat="server">
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:KiwiLogConnectionString %>" SelectCommand="SELECT * FROM [Syslogd]
GROUP BY MsgHostname, MsgDate, MsgTime, MsgPriority, MsgText"></asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="3" DataSourceID="SqlDataSource1" ForeColor="#333333" AllowPaging="True" PageSize="15">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField >
<ItemTemplate >
<asp:CheckBox ID ="Checkbox" runat="server"/>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="MsgDate" HeaderText="Datum" SortExpression="MsgDate" ItemStyle-Wrap="false" >
<ItemStyle Wrap="False"></ItemStyle>
</asp:BoundField>
<asp:BoundField DataField="MsgTime" HeaderText="Tijd" SortExpression="MsgTime" ItemStyle-Wrap="false" >
<ItemStyle Wrap="False"></ItemStyle>
</asp:BoundField>
<asp:BoundField DataField="MsgPriority" HeaderText="Priority" SortExpression="MsgPriority" ItemStyle-Wrap="false" >
<ItemStyle Wrap="False"></ItemStyle>
</asp:BoundField>
<asp:BoundField DataField="MsgHostname" HeaderText="Hostname" SortExpression="MsgHostname" ItemStyle-Wrap="false" >
<ItemStyle Wrap="False"></ItemStyle>
</asp:BoundField>
<asp:BoundField DataField="MsgText" HeaderText="Message" SortExpression="MsgText" />
</Columns>
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<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>
<asp:Button ID="btn_update" runat="server" OnClick="btn_update_Click" Text="Update" />
</form>
</body>
If that CheckBox is checked and the "Update" Button is clicked, i want these row(s) to be hidden. How can i manage to do that?
protected void btn_update_Click(object sender, EventArgs e)
{
}
The gridview is build up with the SQL Database, so it has to be dynamic. In foward, many thanks!

You can try the following:
protected void btn_update_Click(object sender, EventArgs e)
{
foreach (GridViewRow gvr in GridView1.Rows)
{
if (((CheckBox)gvr.findcontrol("Checkbox")).Checked == true)
{
//Do stuff with checked row
gvr.Visible = false;
}
}
}

I would use javascript for something like this.

Related

ASP.NET GridView Needs two Different Action Buttons

I've got a GridView that I'd like to have two buttons with two different actions. I had tried making both of them select buttons, which would be optimal, but I can't get ASP.NET to tell me which of the two buttons triggered the event. It'll tell you the row index, but not the column from what I see.
I changed one of my buttons to an edit button so that it then calls a different method, but then it puts the row into edit mode. I do not see a way to cancel the edit, AND it is mis-using the intended use of the code.
The buttons are in the first and last columns of the gv.
<asp:GridView ID="gvMedList" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3" DataSourceID="dsMedList" GridLines="Vertical" OnSelectedIndexChanged="gvMedAction" OnDataBound="gvMedList_DataBound" OnRowEditing="gvRefillButton">
<AlternatingRowStyle BackColor="#DCDCDC" />
<Columns>
<asp:CommandField ButtonType="Button" SelectText=" -STOP- " ShowSelectButton="True" ShowCancelButton="False" />
<asp:CheckBoxField DataField="Active_Med" HeaderText="Active" SortExpression="Active_Med" >
<HeaderStyle Width="50px" />
<ItemStyle HorizontalAlign="Center" />
</asp:CheckBoxField>
<asp:BoundField DataField="Medication_List_ID" HeaderText="Medication_List_ID" InsertVisible="False" ReadOnly="True" SortExpression="Medication_List_ID" >
<HeaderStyle Width="50px" />
</asp:BoundField>
<asp:BoundField DataField="Label_Name" HeaderText="Medication" SortExpression="Label_Name" >
<HeaderStyle Width="150px" />
</asp:BoundField>
<asp:BoundField DataField="Med_Form" HeaderText="Form" SortExpression="Med_Form" >
<HeaderStyle Width="50px" />
</asp:BoundField>
<asp:BoundField DataField="dose" HeaderText="Dose" SortExpression="dose" >
<HeaderStyle Width="50px" />
</asp:BoundField>
<asp:BoundField DataField="dose_unit" HeaderText="Unit" SortExpression="dose_unit" >
<HeaderStyle Width="50px" />
</asp:BoundField>
<asp:BoundField DataField="Med_Amt" HeaderText="Med_Amt" SortExpression="Med_Amt" Visible="False" />
<asp:BoundField DataField="Amount_Unit" HeaderText="Amount_Unit" SortExpression="Amount_Unit" Visible="False" />
<asp:BoundField DataField="Med_Sched_Label" HeaderText="Frequency" SortExpression="Med_Sched_Label" >
<HeaderStyle Width="150px" />
</asp:BoundField>
<asp:BoundField DataField="Med_Dispense" HeaderText="Dispense" SortExpression="Med_Dispense" >
<HeaderStyle Width="50px" />
</asp:BoundField>
<asp:BoundField DataField="Dispense_Unit" HeaderText="Unit" SortExpression="Dispense_Unit" ShowHeader="False" >
<ItemStyle Width="50px" />
</asp:BoundField>
<asp:BoundField DataField="Med_Refill" HeaderText="Med_Refill" SortExpression="Med_Refill" Visible="False" />
<asp:BoundField DataField="Comments" HeaderText="Comments" SortExpression="Comments" />
<asp:CommandField ButtonType="Button" EditText="-REFILL-" ShowCancelButton="False" ShowEditButton="True" />
</Columns>
<FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
<HeaderStyle BackColor="#27627E" Font-Bold="True" ForeColor="White" BorderColor="White" BorderStyle="Ridge" BorderWidth="2px" Height="20px" HorizontalAlign="Center" VerticalAlign="Middle" Width="125px" />
<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>
Use the CommandName property
Front-End
<asp:GridView ID="gvMedList" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3" DataSourceID="dsMedList" GridLines="Vertical" OnSelectedIndexChanged="gvMedAction" OnDataBound="gvMedList_DataBound" OnRowEditing="gvRefillButton" OnRowCommand="gvMedList_RowCommand">
<Columns>
<asp:TemplateField HeaderText="ColumnName">
<ItemTemplate>
<asp:Button ID="btnDoSomething" runat="server" CommandName="DoSomething" Text="Do Something" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="AnotherColumn">
<ItemTemplate>
<asp:ImageButton ID="btnImageSomething" runat="server" CommandName="DoSomethingElse" ImageUrl="~/images/yes.png" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Code-Behind (assumed C#...if you need VB let me know)
protected void gvMedList_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "DoSomething")
{
// code to execute
}
if (e.CommandName == "DoSomethingElse")
{
// code to execute
}
}

Need to load data from grid view to text box and droup down using Row command

This is my Grid view. I want to know how to bind data to Text boxes and drop down list using Row command event.
<asp:Panel ID="Panel2" runat="server" CssClass="mid">
<asp:GridView ID="BuyerGrid" runat="server" AutoGenerateColumns="False" DataKeyNames="BuyerId" CellPadding="4" ForeColor="#333333" GridLines="None" OnRowCommand="BuyerGrid_RowCommand">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:BoundField DataField="BuyerName" HeaderText="Buyer Name" />
<asp:BoundField DataField="BuyerCode" HeaderText="Buyer Code" />
<asp:BoundField DataField="CountryName" HeaderText="Country" />
<asp:BoundField DataField="CityName" HeaderText="City" />
<asp:TemplateField HeaderText="" SortExpression="">
<ItemTemplate>
<asp:LinkButton ID="LinkButtonEdit" runat="server" CommandName="Edit"
CommandArgument='<%#Eval("BuyerId") %>'>Edit</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</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>
</asp:Panel>
This is my code behind part.
protected void BuyerGrid_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Edit")
{
int index = Convert.ToInt32(e.CommandArgument);
//Bind values in the text box of the pop up control
tbxBuyerName.Text = BuyerGrid.Rows[index].Cells[0].Text;
tbxBuerCode.Text = BuyerGrid.Rows[index].Cells[1].Text;
//.Text = gview.Rows[index].Cells[2].Text;
}
}
Change
CommandArgument='<%#Eval("BuyerId") %>'
to
CommandArgument='<%# Container.DataItemIndex %>'
or
CommandArgument='<%# Container.DisplayIndex %>'

gridview doesn't show update data after click event

i have filled gridview with Store procedure which needs registrationNo parameter so TEXTBOX is control parameter, and that store procedure is on server's database which receives new records from tablet device. It shows new records when i click SEARCH button after entering data in textbox but problem is that if new data is received from tablet and i hit search button then it doesn't show new data, i have to go back to home page then this page and again entering data and clicking search button, then it displays why ?
CODE:
<form id="form1" runat="server">
<div>
<asp:Button ID="btnHome" runat="server" Text="Home" CssClass="button" Width="7%" OnClick="btnHome_Click" />
</div>
<br />
<div>
<asp:Panel ID="pnlInput" runat="server" DefaultButton="btnSearch">
<asp:TextBox ID="txtboxVehicleNo" runat="server"></asp:TextBox> &nbsp
<asp:Button ID="btnSearch" Text="Search" Width="9%" CssClass="button" runat="server" OnClick="btnSearch_Click1" />
</asp:Panel>
</div>
<br />
<br />
<div>
<asp:GridView ID="gvVehicleLedger" ShowHeaderWhenEmpty="True" runat="server" CellPadding="7" DataSourceID="SqlDataSourceETTVehicleLed" ForeColor="#333333" GridLines="None" AllowPaging="True" AutoGenerateColumns="False" DataKeyNames="Vehicle No" Width="100%" BorderColor="#CCCCCC" BorderStyle="Solid" BorderWidth="1px">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField DataField="Vehicle No" HeaderText="Vehicle No" ReadOnly="True" SortExpression="Vehicle No" >
<HeaderStyle HorizontalAlign="Left" Wrap="False" />
<ItemStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="Transaction Date" HeaderText="Transaction Date" SortExpression="Transaction Date">
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle HorizontalAlign="Left" Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="Engine Capacity" HeaderText="Engine Capacity" SortExpression="Engine Capacity" >
<HeaderStyle HorizontalAlign="Left" Wrap="False" />
<ItemStyle HorizontalAlign="Left" Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" >
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" >
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="No Of Months" HeaderText="No Of Months" SortExpression="No Of Months" >
<HeaderStyle HorizontalAlign="Center" Wrap="False" />
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="Amount" HeaderText="Amount" SortExpression="Amount" >
<HeaderStyle HorizontalAlign="Right" />
<ItemStyle HorizontalAlign="Right" />
</asp:BoundField>
</Columns>
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<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" />
<emptydatarowstyle backcolor="#eff3fb"
forecolor="Red"/>
<EmptyDataTemplate>
No Records Found
</EmptyDataTemplate>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSourceETTVehicleLed" runat="server" ConnectionString="<%$ ConnectionStrings:ETTConnectionStr %>" SelectCommand="Vehicleledger" SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:ControlParameter ControlID="txtboxVehicleNo" Name="RegistrationNo" PropertyName="Text" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
</div>
</form>
.cs code:
if(!IsPostBack)
{
gvVehicleLedger.Visible = false;
}
protected void btnSearch_Click1(object sender, EventArgs e)
{
gvVehicleLedger.Visible = true;
}
after searching data you should set grid's data source which is newly data to from search
example
private void serach_click()
{
// your code to retrieve your data
gridview.datasource = your data source;
gridview.databind();
}
looks like you don't refresh your data for the gridView on searchClick. I think you should use smthng like gvVehicleLedger.databind().
(there's a blog about this topic here)

filter gridview using checkbox

I have a gridView and two checkboxes that will be used for filtering..
the first checkbox(Accepted), when checked, the gridview will only show data from the database that have the status Accepted.
Here's my gridview and checkbox:
<div style="height: 250px; overflow-x: hidden; overflow-y: scroll;" >
<asp:CheckBox ID="Accepted" runat="server" />
<asp:CheckBox ID="Pending" runat="server" />
<asp:CheckBox ID="Rejected" runat="server" />
<asp:GridView ID="gvtransaction" runat="server" Width="30%" AutoGenerateColumns="False" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="5px" DataKeyNames="id" GridLines="Horizontal" OnRowDataBound="gvtransaction_RowDataBound" OnRowCommand="TransactionStatus">
<Columns>
<asp:BoundField DataField="MerchantID" HeaderText="ID" SortExpression="" />
<asp:BoundField DataField="FirstName" HeaderText="Consumer" SortExpression="" />
<asp:BoundField DataField="LastName" HeaderText="Name" SortExpression="" />
<asp:BoundField DataField="Amount" HeaderText="Amount" SortExpression="" />
<asp:BoundField DataField="CurrencyName" HeaderText="Account Name" SortExpression="" />
<asp:BoundField DataField="Status" HeaderText="Status" SortExpression="" />
<asp:ButtonField ButtonType="Button" CommandName="Accept" HeaderText="Action" ShowHeader="True" Text="Accept" />
<asp:ButtonField ButtonType="Button" CommandName="Reject" HeaderText="Action" ShowHeader="True" Text="Reject" />
</Columns>
<FooterStyle BackColor="White" ForeColor="#000066" />
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
<RowStyle ForeColor="#000066" />
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#007DBB" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#00547E" />
</asp:GridView>
How can i do this using javascript.?
thank you...
You can use following properties of gridview 'onrowdatabound and DataKeyNames'.
In DataKeyNames you can give the name of your class variables or properties where you are reading from database through a datareader.
Fire the rowDatabound event of your gridview so that you can check the status
protected void gvtransaction_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
int Status = (int)this.gvtransaction.DataKeys[e.Row.RowIndex].Values[0];
if (Status)
{
.
check status values for Accepted, pending and rejected through if else.
.
.
}
}
You can create 2 Grid views and when one of the check boxes selected bind the associated grid view.

I can not edit in the gridview

I am using linq to entity i have added the gridview but it's not editing and when i debug it doesn't access the method GridView5_RowUpdating().. here it's the code of the grid view
<asp:GridView ID="GridView5" runat="server" AllowSorting="True"
AutoGenerateColumns="False" CellPadding="4" DataKeyNames="CustomerId"
DataSourceID="SqlDataSource3" ForeColor="#333333" GridLines="None"
onrowupdating="GridView5_RowUpdating">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
<asp:BoundField DataField="CustomerId" HeaderText="CustomerId"
InsertVisible="False" ReadOnly="True" SortExpression="CustomerId" />
<asp:BoundField DataField="FirstName" HeaderText="FirstName"
SortExpression="FirstName" />
<asp:BoundField DataField="LastName" HeaderText="LastName"
SortExpression="LastName" />
<asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
</Columns>
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
<SortedAscendingCellStyle BackColor="#FDF5AC" />
<SortedAscendingHeaderStyle BackColor="#4D0000" />
<SortedDescendingCellStyle BackColor="#FCF6C0" />
<SortedDescendingHeaderStyle BackColor="#820000" />
</asp:GridView>
I have Required field validator when i am trying to insert manually when i removed the Required field validator can someone tell me what's happening?
Firstly you need to implement the RowEditing event on the gridview to enable editing, MSDN definition:
Occurs when a row's Edit button is clicked, but before
the GridView control enters edit mode.
ASPX:
<asp:GridView
ID="gvCustomers"
runat="server"
OnRowEditing="gvCustomers_RowEditing">
Code behind:
protected void gvCustomers_RowEditing(object sender, GridViewEditEventArgs e)
{
gvCustomers.EditIndex = e.NewEditIndex;
//Re bind the grid view
}
Now if this event is not being fired when you click the Edit link on your grid view it means that there is some validation logic on the page which is preventing a post back.
The best way to get rid of this issue is to set the ValidationGroup attribute, for the validation controls causing trouble(not the gridview):
<div id="insertEmployee">
<asp:TextBox ID="txtName" runat="server" ValidationGroup="Insert" />
<asp:RequiredFieldValidator ID="rfvName" runat="server" ControlToValidate="txtName" ErrorMessage="Name is required" ValidationGroup="Insert" />
<asp:Button ID="btnAdd" runat="server" Text="Add" ValidationGroup="Insert" />
</div>

Resources