I want get the rows I selected from gridview use a checkbox.
The checkbox is like this!
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server"
oncheckedchanged="CheckBox1_CheckedChanged" />
</ItemTemplate>
</asp:TemplateField>
And I want to get one column in each row.
How to do it.thx!
try this:
protected void CheckBox1_CheckedChanged(object sender, System.EventArgs e)
{
CheckBox checkbox = (CheckBox)sender;
GridViewRow row = (GridViewRow)checkbox.NamingContainer;
if (checkbox.Checked == true) {
row.BackColor = System.Drawing.Color.Red;
mygridview.Columns(0).Visible = false;
}
}
You can loop through the GridView rows and use FindControl to retrieve the Checkbox and then get the IsChecked property on them.
foreach (GridViewRow row in grid.Rows)
{
CheckBox check = (CheckBox)row.FindControl("CheckboxID");
if (CheckBox1.Checked)
{
...
}
}
Related
I have a gridview which displays a data from the database. In one of the tables in this database have a column to store details about attanchment file. If the attachment is available that column value will set as "YES". Otherwise it will set as "NO". What I want to do is, show a link to view the attachment. But if cell value of the column is "NO" (when there is no attachment) the link must be hidden.
Note : I know how to view a file. Here what Im expecting is to hide the link in the cell which doesn't have an attachment.
This is what I have done upto now.
if (ds.Tables[0].Rows.Count > 0)
{
grdSo.DataSource = ds;
grdSo.DataBind();
for(int i=0; i <ds.Tables[0].Rows.Count; i++)
{
if (ds.Tables[0].Rows[i][6].Equals("NO"))
{
grdSo.Rows[i].Cells[6].Visible = false;
}
else
{
grdSo.Rows[i].Cells[6].Visible = true;
}
}
}
I could hide the cell using this code. But unfortunately this hides the lines of the cell too. How can I avoid it happening?
One of the ways to do this is to use server side control like LinkButton to view the link that you want to show and set the visibility of the control as per your requirement in the OnRowDataBound event of the gridview.
Below is the code to show/hide LinkButton on OnRowDataBound event.
protected void gridId_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataSourceClass varData = (DataSourceClass)e.Row.DataItem;
// check if your data have flag to show the link
if(varData.show)
((LinkButton)e.Row.FindControl("linkbuttonId")).Visible = true;
else
((LinkButton)e.Row.FindControl("linkbuttonId")).Visible = false;
}
}
ASPX Code :
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:BoundField Visible="false" DataField="id" />
<asp:TemplateField HeaderText="Has Attachment">
<ItemTemplate>
<asp:Label ID="lblAtt" runat="server" Text='<%#Eval("HasAtt") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="View Attachment">
<ItemTemplate>
<asp:LinkButton ID="lbtnAtt" runat="server" OnClick="lbtnAtt_Click" Visible="false">View</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
CS Code :
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
this.BindGrid();
}
}
private void BindGrid()
{
//Here you write databind logic
// Datasource table of GridView1 should contain 'HasAtt' and 'id' as its binded to label.
}
private void ViewAttachment(int id)
{
//Here you write your logic to view attachment.
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow) // checking if row is datarow or not
{
Label lblHasAtt = e.Row.FindControl("lblAtt") as Label;
LinkButton lbtnViewAtt = e.Row.FindControl("lbtnAtt") as LinkButton;
lbtnViewAtt.Visible = (lblHasAtt.Text.ToLower() == "yes");
}
}
protected void lbtnAtt_Click(object sender, EventArgs e)
{
LinkButton lbtnViewAtt = sender as LinkButton;
GridViewRow grw = lbtnViewAtt.NamingContainer as GridViewRow;
int id = Convert.ToInt32(this.GridView1.Rows[grw.RowIndex].Cells[0].Text); // retriving value of first column on 'lbtnAtt' click row.
this.ViewAttachment(id);
}
I recall being able to parse through the grid view by using a for each for the rows and using a for statement for columns.
If I recall correctly, you can grab an item ie
row.Item[i]
foreach(GridViewRow row in GridView1.Rows)
{
for(int i = 0; i < GridView1.Columns.Count; i++)
{
// here you can do the logic to decide whether to show or hide the text for this cell
}
}
Sorry for formatting responding on my phone.
I have GridView filled automatically as
<asp:GridView ID="gvValues" runat="server"
OnRowDataBound="gvValues_RowDataBound"
OnPageIndexChanging="gvValues_PageIndexChanging"
<Columns>
<asp:TemplateField HeaderText="#">
<ItemTemplate>
<%# gvValues.PageSize*gvValues.PageIndex+ Container.DisplayIndex+1 %>
<asp:CheckBox ID="chkProduct" runat="server" CssClass="chkProduct"/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="online" meta:resourcekey="Online">
<ItemTemplate >
<asp:CheckBox ID="chkProductonline" runat="server" OnCheckedChanged ="chkProductonline_CheckedChanged" AutoPostBack="true"/>
</ItemTemplate>
</asp:TemplateField>
What I need is when I click on the chkProductonline checkbox, to fire an event and get the chkProductonline and chkProducton values. I have tried this but it always gives me null.
protected void chkProductonline_CheckedChanged(object sender, EventArgs e)
{
var chkProductonline = FindControl("chkProductonline") as CheckBox;
// bool ischeck = chkProductonline.Checked;
var chkProduct = gvValues.FindControl("chkProduct") as CheckBox;
}
I can't loop the GridView. I need to do this one-by-one. Is there another way to do that?
You can try this:
protected void chkProductonline_CheckedChanged(object sender, EventArgs e)
{
CheckBox chkProductonline = sender as CheckBox;
...
CheckBox chkProduct = chkProductionLine.NamingContainer.FindControl("chkProduct") as CheckBox;
...
}
You need to call FindControl on a specific row. You will not be able to call it on the entire GridView because there exists repeating content (i.e. multiple chkProductionlines and chkProducts). A row knows of its checkboxes, and not of the others.
So what you can do is first get the CheckBox that called the event (your sender parameter, chkProductionline) and use its NamingContainer. Since it is contained in a GridView row, cast the row as such as use it to find the other controls you may need.
protected void chkProductonline_CheckedChanged(object sender, EventArgs e)
{
CheckBox chkProductionline = (CheckBox)sender;
GridViewRow row = (GridViewRow)chkProductionline.NamingContainer;
CheckBox chkProduct = (CheckBox)row.FindControl("chkProduct");
}
I have a GridView. Every row has a textbox and a radiobutton (3 options)
If the radiobutton is selected then the textbox.text = ""
Problem: when OnSelectedIndexChanged is called every textbox inside my grid goes blank
How can I clear only the textbox of the row I selected the radiobutton in?
ASPX markup
<asp:GridView id="mygrid" Runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:RadioButtonList ID="hi" runat="server"
OnSelectedIndexChanged="zzz" AutoPostBack="true" />
<asp:TextBox ID="txPregoeiro" runat="server" Text="." />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
C# code-behind
protected void zzz(object sender, EventArgs e)
{
foreach (GridViewRow _row in mygrid.Rows)
{
if (_row.RowType == DataControlRowType.DataRow)
{
RadioButtonList hi = (RadioButtonList)_row.FindControl("hi");
TextBox txPregoeiro = (TextBox)_row.FindControl("txPregoeiro");
txPregoeiro.Text = string.Empty;
}
}
}
Your not checking to see if the radio button list has a selected item or not. As a result you are always setting the textbox text to blank. Change the function to:
GridViewRow _row = mygrid.SelectedRow;
if (_row.RowType == DataControlRowType.DataRow)
{
RadioButtonList hi = (RadioButtonList)_row.FindControl("hi");
if(hi.SelectedItem != null) //This checks to see if a radio button in the list was selected
{
TextBox txPregoeiro = (TextBox)_row.FindControl("txPregoeiro");
txPregoeiro.Text = string.Empty;
}
}
You are currently doing it for every row which will clear every text box. Give this a try.
protected void zzz(object sender, EventArgs e)
{
var caller = (RadionButtonList)sender;
foreach (GridViewRow _row in mygrid.Rows)
{
if (_row.RowType == DataControlRowType.DataRow)
{
RadioButtonList hi = (RadioButtonList)_row.FindControl("hi");
if(hi == caller)
{
TextBox txPregoeiro = (TextBox)_row.FindControl("txPregoeiro");
txPregoeiro.Text = string.Empty;
break; //a match was found break from the loop
}
}
}
}
I wrote a code that enabled the use of checkbox with gridview, and it worked fine. I was afterward told to modify it, to enable a checkbox to check all. Please how do I go about it in C#. I have checked with Google and this site and cannot find any thing that can help my situation.
You should use OnSelectedIndexChanged event.
Add your checkbox:
<asp:CheckBoxList ID="CheckBoxList1" runat="server" AutoPostBack="true"
DataSourceID="SqlDataSource1" DataTextField="name" DataValueField="name"
OnSelectedIndexChanged="CheckBoxList1_SelectedIndexChanged">
</asp:CheckBoxList>
In order to enable another checkbox you can use below code part.
protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
{
//anotherCheckbox.Checked = true;
}
In order to check all items below code part can help you.
for(int i = 1; i < anotherCheckbox.Items.Count; i++)
{
anotherCheckbox.SetItemChecked (i, true);
}
And you should call this loop from SelectedIndexChanged.
Try this
<asp:CheckBox id="chkBox" runat="server"
AutoPostBack="True"
Text="check all"
OnCheckedChanged="Check_Clicked"/>
And
protected void Check_Clicked(object sender, EventArgs e)
{
if (chkBox.Checked == true)
{
foreach (GridViewRow gvr in MyGridview.Rows)
{
//Programmatically access the CheckBox from the TemplateField
CheckBox cb = (CheckBox)gvr.FindControl("RowLevelCheckBox");
//Check it!
cb.Checked = true;
}
}
}
loop through all rows in the gridview and set the value for the checkbox column to true.
foreach(DataGridViewRow row in dgv.Rows)
{
row.Cells[0].Value = true;
}
something like that.
I have a grid with a few items which have a checkbox at the start of each row. I also have a select all checkbox at the top of the grid. The scenario is something like our gmail or yahoo inbox. My question is, suppose i disable a checkbox and then click on select all checkbox, the disabled checkbox should not be checked. Is there any solution for this? I have attached relevant pieces of code as follows.
*aspx file
<telerik:GridTemplateColumn UniqueName="CheckBoxTemplateColumn">
<HeaderTemplate>
<asp:CheckBox id="headerChkbox" OnCheckedChanged="ToggleSelectedState" AutoPostBack="True" runat="server"></asp:CheckBox>
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox id="CheckBox1" OnCheckedChanged="ToggleRowSelection" AutoPostBack="True" runat="server"></asp:CheckBox>
</ItemTemplate>
</telerik:GridTemplateColumn>
*code behind
protected void ToggleRowSelection(object sender, EventArgs e) //selecting a particular row
{
((sender as CheckBox).NamingContainer as GridItem).Selected = (sender as CheckBox).Checked;
}
protected void ToggleSelectedState(object sender, EventArgs e) //select all rows
{
CheckBox headerCheckBox = (sender as CheckBox);
foreach (GridDataItem dataItem in grdCurrent.MasterTableView.Items)
{
(dataItem.FindControl("CheckBox1") as CheckBox).Checked = headerCheckBox.Checked;
dataItem.Selected = headerCheckBox.Checked;
}
}
You need to check if it is Enabled before setting it to Checked.
CheckBox headerCheckBox = (sender as CheckBox);
foreach (GridDataItem dataItem in grdCurrent.MasterTableView.Items)
{
if((dataItem.FindControl("CheckBox1") as CheckBox).Enabled) // add this condtion
{
(dataItem.FindControl("CheckBox1") as CheckBox).Checked = headerCheckBox.Checked;
dataItem.Selected = headerCheckBox.Checked;
}
}
Instead of using code behind to select all. I will use javascript or Jquery to do it. My method will be applying different css class for enabled and disabled checkbox like "enabled" and "disabled". In Jquery, I check the class and only tick the enabled checkboxes.