How to Get only checkedBox rows from gridView - c#

I have some gridView with checkBox option, I want to get only CheckedRows and make a new GridView with them.
GridView:
<asp:GridView ID="GridViewMediaAndSource" runat="server" DataSourceID="SqlDataSourceMedia"
CssClass="mGrid"
PagerStyle-CssClass="pgr" AlternatingRowStyle-CssClass="alt"
PageSize="15" AllowPaging="True" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="CheckBoxSelect" runat="server"></asp:CheckBox>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="MediaType" HeaderText="Източник" />
<asp:BoundField DataField="mediaName" HeaderText="Име на медията" />
</Columns>
</asp:GridView>
How to make new GridView with only data which was cheked from this view

You didn't provided your code so, I hope this give you a start.
for(int i=0; i< Grid.Rows.Count(); i++)
{
CheckBox check = (CheckBox)Grid.Rows[i]["CheckColumnName"].Controls[0];
if(check.Checked)
{
//do something
}
}

Related

Grid view Link button not firing on click asp.net?

Grid view Link button (one of the column) onclick not firing. Really unable to understand what am doing wrong. need help. rookie in coding.
.aspx
<asp:GridView ID="gridTSRJOBList" runat="server" AutoGenerateColumns="False" CssClass="table table-striped table-bordered dataTable print-first-column-hide" CellSpacing="0" Width="100%" ShowHeaderWhenEmpty="True" EmptyDataText="No TSR JOB Found for this Lease" GridLines="None" DataKeyNames="JobID"role="grid">
<Columns>
<asp:TemplateField HeaderText="Job" ItemStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:LinkButton runat="server" ID="lnkJobID" CommandArgument='<%# Eval("JobID") %>' Text='<%#Eval("JobID") %>' OnClick="lnkJobID_Click"></asp:LinkButton></ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="CategoryID" HeaderText="Category" ReadOnly="True" />
<asp:BoundField DataField="JobDetail" HeaderText="Short Description" ReadOnly="True" />
<asp:BoundField DataField="JobType" HeaderText="Job Status" ReadOnly="True" />
<asp:BoundField DataField="CustomerReference" HeaderText=" Customer Reference" ReadOnly="True" />
</Columns>
</asp:GridView>
C# code
protected void lnkJobID_Click(object sender, EventArgs e)
{
try {
LinkButton jobLinkbutton = new LinkButton();
jobLinkbutton = (LinkButton)sender;
Response.Redirect("TSR_proc.aspx?bl=1&JOBID=" + jobLinkbutton.CommandArgument);
} catch (Exception ex) {
}
}
take off lnkJobID_Click from linkbutton and add CommandName='clickLink'.
You can manage this event inside the Gridview RowCommand Event, in your case
gridTSRJOBList_RowCommand

Select complete rows from a gridview using a checkbox and transfer to another webform

I need help to select information from a gridview not to delete, I´ve seen multiple ways to delete and I've tried to adapt it to my code but I'm having some issues with it,
This is my gridview code
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ItemStyle-Width="150px" ID="cbSelect" runat="server" AutoPostBack="True" OnCheckedChanged="cbSelect_CheckedChanged" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ItemStyle-Width="150px" ID="cbSelected" runat="server" AutoPostBack="True" OnCheckedChanged="cbSelected_CheckedChanged" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Poliza">
<ItemTemplate>
<asp:Label ID="Poliza" runat="server" Text='<%# Bind("POLIZA") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField ItemStyle-Width="150px" DataField="RAMO"
HeaderText="Ramo" />
<asp:BoundField ItemStyle-Width="150px" DataField="CERTIF"
HeaderText="Certificado" />
<asp:BoundField ItemStyle-Width="150px" DataField="NOMRMO"
HeaderText="Ramo" />
<asp:BoundField ItemStyle-Width="150px" DataField="CIA"
HeaderText="N. Comp" />
<asp:BoundField ItemStyle-Width="150px" DataField="NOMCIA"
HeaderText="Compañia" />
<asp:BoundField ItemStyle-Width="150px" DataField="RIF"
HeaderText="Ident" />
<asp:BoundField ItemStyle-Width="150px" DataField="CCT"
HeaderText="Num" />
</Columns>
</asp:GridView>
In my backend I have the code that fills the gridview which is working perfectly, also the code that evaluates if the checkbox has been checked.
Now, this is the code of my button that needs to pick the value of all the rows and transfer to another webform, normally I have done it with a "buttonfield" with "OnSelectedIndexChanged" but I can I do it with a checkbox?
protected void Button2_Click(object sender, EventArgs e)
{
List<string> test = new List<string>();
foreach (GridViewRow gridViewRow in GridView2.Rows)
{
if (((CheckBox)gridViewRow.FindControl("cbSelected")).Checked)
{
string ejecutivoId = ((Label)gridViewRow.FindControl("Poliza")).Text;
test.Add(ejecutivoId);
}
}
if (test.Count > 0)
{}
Yes you can.
Fills the gridview and do the same as you did with Button Click, but with theOnCheckedChanged event.

Getting value of a cell from a GridView on SelectedIndexChanged event of DropDownList

I have a GridView that has data bounded rows. I'm trying to get specific cell value on the SelectedIndexChanged event of DropDownList. My tries are as follows:
string temp= GridView2.SelectedRow.Cells[3].Text;
string temp = ((DataBoundLiteralControl)GridView2.Rows[0].Cells[3].Controls[0]).Text;
DataBoundLiteralControl dblc = (DataBoundLiteralControl)GridView2.Rows[0].Cells[3].Controls[0];
string temp=dblc.Text;
These all 3 of them returns null.
Moreover, the Control[0] is returning correct value of TemplateFields only, but not DataBound fields.
.aspx
<asp:GridView ID="GridView1" runat="server" HeaderStyle-BackColor=" #d54d7b" HeaderStyle-ForeColor="White"
RowStyle-BackColor="#FFFAFC" AlternatingRowStyle-BackColor="#FFFFF7" AlternatingRowStyle-ForeColor="#000"
AutoGenerateColumns="false" AllowPaging="true" OnPageIndexChanging="OnPageIndexChanging" Width="900px" Font-Names="Segoe UI Light" BorderColor="#DEDEDE">
<RowStyle HorizontalAlign="center" />
<Columns>
<asp:BoundField DataField="Description" HeaderText="Description" ItemStyle-Width="80" />
<asp:BoundField DataField="Vacancies" HeaderText="Vacancies" ItemStyle-Width="150" />
<asp:TemplateField HeaderText="Detail">
<ItemTemplate>
<asp:BoundField DataField="Date" HeaderText="Date" ItemStyle-Width="80" />
<asp:BoundField DataField="Time" HeaderText="Time" ItemStyle-Width="80" />
</Columns>
Problem you are probably facing is that, GridView1 hasn't been bounded yet.. that is the reason why Control[0] is returning correct value of TemplateFields but cells in the gridview returns null.
You can do the following
protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
{
/// send value to filter or bind gridView1
/// bind gridvew
string temp = GridView1.Rows[0].Cells[3].Text;
/// or any code to get values from GridView Cell
}
I Converted all <asp:BoundFields> to <asp:TemplateField> and it looked like this:
<asp:GridView ID="GridView1" runat="server" HeaderStyle-BackColor=" #009C0D" HeaderStyle-ForeColor="White"
RowStyle-BackColor="#FFFAFC" AlternatingRowStyle-BackColor="#FFFFF7" AlternatingRowStyle-ForeColor="#000"
AutoGenerateColumns="false" AllowPaging="true" CssClass="test"
HtmlEncode="true"
Font-Names="Segoe UI Light" BorderColor="#DEDEDE" >
<RowStyle HorizontalAlign="center" />
<Columns>
<asp:TemplateField HeaderText="Description" ControlStyle-Width="250px" ><ItemTemplate> <asp:LinkButton ID= "Description" PostBackUrl='<%# Eval("Description", "~/{0}.aspx") %>' Text='<%# Eval("Description")%>' runat="server" ></asp:LinkButton> </ItemTemplate></asp:TemplateField>
<asp:TemplateField HeaderText="Vacancies"><ItemTemplate> <asp:Label ID="Vacancies" Text='<%# Eval("Vacancies")%>' runat="server" ></asp:Label> </ItemTemplate></asp:TemplateField>
<asp:TemplateField HeaderText="Detail"><ItemTemplate><asp:Label ID="City" Text='<%# Eval("City")%>' runat="server" ></asp:Label> <div style="font-size:10px"> <asp:Label ID="Label1" Text='<%# Eval("DateDay")%>' runat="server" ></asp:Label> </div> </ItemTemplate></asp:TemplateField>
//and others
</Columns>
</asp:GridView>
And get specific value by entering this code behind DropDownList:
if (dropdown.SelectedIndex != -1)
{
ListItem mySelectedItem = (from ListItem li in dropdown.Items where li.Selected == true select li).First();
foreach (GridViewRow rw in GridView2.Rows)
{
Label tv = (Label)rw.Cells[3].FindControl("City");
if (tv.Text.IndexOf(mySelectedItem.Text) != -1)
{
rw.Visible = true;
}
else
rw.Visible = false;
}
}

How to access a selected Boundfield value from a GridView in code-behind

I have seen similar questions but none of the answers helped me to workout this problem.
I have GridView with a ReadOnly field as follow.
GridView:
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AutoGenerateColumns="False" DataKeyNames="projectID"
DataSourceID="SqlDataSource1"
EmptyDataText="There are no data records to display."
PageSize="5" OnRowUpdating="GridView1_RowUpdating">
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True"/>
<asp:BoundField DataField="prID" HeaderText="prID" SortExpression="prID"/>
<asp:BoundField DataField="projectName" HeaderText="projectName"
SortExpression="projectName" />
<asp:BoundField DataField="projectType" HeaderText="projectType"
SortExpression="projectType" />
</Columns>
<EditRowStyle CssClass="GridViewEditRow"/>
</asp:GridView>
as you can see the prID BoundField has Readonly=True attribute.
I'm trying to get the value of the prID in code-behind when user is updating the other fields in the row.
code-behind:
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = GridView1.Rows[e.RowIndex];
String d1 = ((TextBox)(row.Cells[2].Controls[0])).Text;
String d2 = ((TextBox)(row.Cells[3].Controls[0])).Text;
// this only works while the field is not readonly
string prIDUpdate = ((TextBox)(row.Cells[1].Controls[0])).Text;
}
Note: I have tried using GridView1.DataKeys[e.RowIndex] and also onRowDataBound an d setting the BoundField ready only in code-behind but I haven't been able to get results
Thanks in advance!
I saw that your DataKeyNames setting in GridView control is like this
DataKeyNames="projectID"
Then I guess that your key name is projectID not prID, isn't it?
If so, you could get data for the selected row as this line:
string id = GridView1.DataKeys[e.RowIndex]["projectID"].ToString();
And you should also add this column:
<asp:BoundField DataField="projectID" HeaderText="prID" SortExpression="projectID"/>
Did you try that?
In other way, you could try to use TemplateField instead
<Columns>
<asp:TemplateField HeaderText="prID" SortExpression="prID">
<ItemTemplate>
<asp:Label ID="lblPrId" runat="server" Text='<%# Bind("prID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="projectName" HeaderText="projectName"
SortExpression="projectName" />
<asp:BoundField DataField="projectType" HeaderText="projectType"
SortExpression="projectType" />
</Columns>
And this code to get data from prID column in GridView1_RowUpdating event handler:
Label lblPrId = row.FindControl("lblPrId") as Label;
string prId = lblPrId .Text;
Sorry if this doesn't help.

Why is Gridview footer adding an extra column?

Can't believe I have to ask this - you'd think such a basic feature would be straightforward to implement, but I'm having trouble creating a footer for a Gridview. I've checked out various tutorials and other questions, such as here, and here and here, but am still encountering difficulties.
The problem is in displaying the footer properly (i.e. without adding an extra empty column). From what I gather, you need to put the FooterTemplate inside a TemplateField tag or it won't work - at least it wouldn't compile for me. If I insert this after the BoundFields columns then it adds an extra column which is not desirable.
<asp:GridView ID="gridview1" runat="server" AutoGenerateColumns="false" AllowSorting="true"
CellPadding="3" HorizontalAlign="Center" GridLines="both" CssClass="dataTable1"
OnRowDataBound="Colour_Columns" Caption="PARTIAL COMPARE" ShowFooter="true">
<HeaderStyle BackColor="Black" ForeColor="AntiqueWhite" Height="30" CssClass="header" />
<FooterStyle BackColor="Black" ForeColor="AntiqueWhite" Height="30" CssClass="footer" />
<Columns>
<asp:BoundField DataField="FOLDER" HeaderText="Location" />
<asp:BoundField DataField="FILE" HeaderText="File" />
<asp:BoundField DataField="CHECKSUM" HeaderText="Checksum" Visible="false" />
<asp:BoundField DataField="STATUS" HeaderText="Status" />
<asp:BoundField DataField="DATE" HeaderText="Date" Visible="false" />
<asp:TemplateField>
<FooterTemplate>
<asp:Button ID="UpdateButton" runat="server" Text="UPDATE" CssClass="updateButton" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Similarly, if I put it before the BoundFields it adds an extra column on the left. If I try to put all the BoundFields under the TemplateField it won't compile.
How do I add the footer to the gridview without creating an extra column? Also, while we're at it, how can I set its colspan to 1? (It's only going to have the one Update button in it, so no need for three columns in the footer.)
Colour-Scheme method:
protected void Colour_Columns(Object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.Cells[3].Text == "Match")
e.Row.BackColor = Color.Lime;
if (e.Row.Cells[3].Text == "Mismatch")
e.Row.BackColor = Color.Gold;
if (e.Row.Cells[3].Text == "New File")
e.Row.BackColor = Color.PeachPuff;
}
}
This method does not seem to recognize ItemTemplate values...
Try using template field only for the last column and in that column you can specify the ItemTemplate and FooterTemplate. Try the code below.
<asp:GridView ID="gridview1" runat="server" AutoGenerateColumns="false" AllowSorting="true"
CellPadding="3" HorizontalAlign="Center" GridLines="both" CssClass="dataTable1"
OnRowDataBound="Colour_Columns" Caption="PARTIAL COMPARE" ShowFooter="true">
<HeaderStyle BackColor="Black" ForeColor="AntiqueWhite" Height="30" CssClass="header" />
<FooterStyle BackColor="Black" ForeColor="AntiqueWhite" Height="30" CssClass="footer" />
<Columns>
<asp:BoundField DataField="FOLDER" HeaderText="Location" />
<asp:BoundField DataField="FILE" HeaderText="File" />
<asp:BoundField DataField="CHECKSUM" HeaderText="Checksum" Visible="false" />
<asp:TemplateField HeaderText="Status">
<ItemTemplate>
<asp:Label ID="StatusLabel" runat="server" Text='<%# Eval("STATUS") %>' />
</ItemTemplate>
<FooterTemplate>
<asp:Button ID="UpdateButton" runat="server" Text="UPDATE" CssClass="updateButton" />
</FooterTemplate>
</asp:TemplateField>
<asp:BoundField DataField="DATE" HeaderText="Date" Visible="false" />
</Columns>
</asp:GridView>
I changed the Cs file to read the value from the template field. Please recopy the ASPX to becouse I changed it too by adding an ID to the Label
CS:
protected void Colour_Columns(Object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label StatusLabel = e.Row.FindControl("StatusLabel") as Label;
if (StatusLabel.Text == "Match")
e.Row.BackColor = Color.Lime;
if (StatusLabel.Text == "Mismatch")
e.Row.BackColor = Color.Gold;
if (StatusLabel.Text == "New File")
e.Row.BackColor = Color.PeachPuff;
}
}
You shouldn't use FooterTemplate with BoundField. Footer templates are meant to be used in conjunction with TemplateField.Additionally footer templates are applied per column this is done so you can aggregate totals at the bottom of the gridview in cases where you have numeric data.
Here's an example of using template fields with a footer field in the first column, you can change this as required to suit your needs:
ASPX:
<asp:GridView ID="gridview1" runat="server" AutoGenerateColumns="false" ShowFooter="true">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblFolder" runat="server" Text='<%# Eval("FOLDER") %>' />
</ItemTemplate>
<FooterTemplate>
Footer content displayed under FOLDER, notice no extra column!
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblFile" runat="server" Text='<%# Eval("FILE") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblCheck" runat="server" Text='<%# Eval("CHECKSUM") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Result:
Alternatively, you can stick with using BoundFields and add the footer dynamically in code:
protected void gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Footer)
{
var footer = new Label();
footer.Text = "Footer content";
//Footer will be displayed under the *first* column
e.Row.Cells[0].Controls.Add(footer);
}
}
How to code this form:
 
PURCHASE ORDER MASTER
    [lblPONumberH]                       
PO Number ://here is textBox //RequireField                  
PO Date : //here istextbox with Calender extender                
Vendor: //here is dropdown
                       Created By ://here is textbox
save//Button Cancel//Button
GridView
                               
Item Description Budget Number Quantity UOM Price
Databound DataBound DataBound DataBound DataBound Edit & Del btn
in footer style
txtItemDescription ddlBnumber txtQuantity ddlUOM txtPrice AddBtn

Categories