How to disable the CheckBox in the GridView Header? - c#

In this GridView, I want to disable all the CheckBox in the GridView during the View Mode.
I can disable the GridView Row cells like
foreach (GridViewRow GVR in gvPODetails.Rows)
{
GVR.Cells[1].Enabled =
GVR.Cells[11].Enabled = false;
}
But I dont know how to disable the checkbox in the HeaderTemplate of the Template Field. How to do this?

Recipe:
Handle OnRowDataBound on your GridView
Detect whether you are iterating through the header by using e.Row.RowType == DataControlRowType.Header and get a reference to your checkbox control using e.Row.FindControl(checkBoxID)
Set the Enabled property to False

Try this code:
void GridView_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Header ) {
MyClass myObj = (myObj)e.Row.DataItem;
CheckBox cb = (CheckBox)e.Row.FindControl("myCheckBox");
cb.Enabled=false;
}
}
And if u want to do using javascript then click here
http://forums.asp.net/t/1742352.aspx/1?How+to+enable+and+disable+CheckBox+in+the+GridView+using+JavaScript+

Related

onRowdatabound not giving label value in asp.net

I am creating the dynamic grid and adding textbox and label in row dynamically on some button click and bind some data from the database to textbox and label.I want to change the label text color and text box read only for condition depend on database.i have used onRowDataBound event of gridview but not getting any value in the textbox and label for a row. Can anyone helps me to solve this issue? Thanks
protected void grdMasterData_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label status = e.Row.FindControl("lblProduct") as Label;
if (status.Text == "LY Actuals")
{
e.Row.Cells[0].CssClass = "lblProductColor";
// lbtAction.Visible = false;
}
}
}
In RowDataBound three row types provided by grid, you are using only DataRow, try to get your values from empty row and footer row because first record will not come in DataRow.

Hide gridview cell (value) base on other cell value

I have a Gridview with ItemTemplate using some Labels and 3 Button (Deactivate, Delete and Edit) as seen in picture below:
I want to hide these buttons from some users base on their username (Label UserName on Gridview), for example:
If UserName == "some string" then hide the Deactivate, Delete and Edit buttons
How do I do this in code behind RowDataBound event?
Yes by using Gridview RowDataBound event you can do that
protected void grd1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lbl=e.Row.FindControl("your cotrol Id")as Label;
if(lbl!=null && lbl.Text.Trim()=="some string")
{
e.Row.FindControl("deactivate btn Id").Visible = false;
e.Row.FindControl("delete btn Id").Visible = false;
e.Row.FindControl("edit btn Id").Visible = false;
}
}
}
You can get the row data by column index, in the RowDataBound event you need to check if its a data row, not a header or footer..etc
if(e.Row.RowType == DataControlRowType.DataRow)
{
if(e.Row.Cells[2].Text = "some string")
{
Button delete = (Button)e.Row.FindControl("control id to hide");
delete.Visisble = false;
}
}

Checkbox always returns true when it is unchecked

I have nested Gridview and the last Gridview has Checkbox.
the condition in checking the Checkbox whether it is checked or unchecked
foreach (GridViewRow gvr in GridView1.Rows)
{
GridView GridView2= gvr.FindControl("GridView2") as GridView;
foreach (GridViewRow gvr2 in GridView2.Rows)
{
GridView GridView3= gvr2.FindControl("GridView3") as GridView;
foreach (GridViewRow gvr3 in GridView3.Rows)
{
if(((CheckBox)gvr3.FindControl("chk1")).Checked)
{
string txt = txtKeyboard.text;
}
}
}
}
even though it is unchecked it will go though the condition
if(((CheckBox)gvr3.FindControl("chk1")).Checked)
{
string txt = txtKeyboard.text;
}
instead of above code just try once
CheckBox chk1 = gvr3.Cells[0].Controls[0] as CheckBox;
if(chk1.checked==true)
{
string txt = txtKeyboard.text;
}
here you need to set cell value and control value inplace of 0
How you bind data to "GridView2" control? if you called some methods like "GridView2_DataBind" , after postback data has be processed, the state of girdview will be reset.
for example,
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack){
GridView2_DataBind() // you shouldn't call databind method when postback
}
}

GridView column with checkbox field with SQL

I have GridView with 2 columns.
The first column is: test-label (TemplateField)
The second: checkbox (asp:CheckBoxField) that connect to sql table with bit column (done).
I want that on page load - the page will check every row, where the checkbox = true, the test-label.visble will be false.
I know how to write code with SELECT statement to check the value from the SQL table, but don't know how to check every row on the gridview on the page-load.
how can I do that?
(i can't use findcontroll for the checkbox because it's checkboxfield and not just "checkbox".
<asp:CheckBoxField DataField="done" SortExpression="done" HeaderText="done?" />
so, what can I do here? maybe to replace that field with regular cb? (i don't know how to do there databind - on the regular cb).
you can use GridView.RowDataBound Event
so you can do something like
protected void GVRowDataBound(object sender, GridViewRowEventArgs e)
{
var check = (CheckBox) e.Row.FindControl("ID"); // ID is id of the checkbox
var lable = (Label) e.Row.FindControl("LableID");
if(check != null && lable != null)
{
if(check.Checked)
{
lable.Visible = false;
}
}
}
You can't do it in Page.Load because the GridView isn't databound yet.
Try handling GridView.RowDataBound.
Code:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
CheckBox cb = (CheckBox)e.Row.FindControl("checkbox");
Label lbl = (Label)e.Row.FindControl("test-label");
lbl.Visible = !(cb.Checked);
}
}

Why do I receive old data in my gridview on page load?

The process I am doing is if lbl.Text is "Validated" then disable the checkbox accordingly in grid. The code works fine if paging is not there.
Now the problem is I am using paging and when I click to the next page of grid
the validated things appear with the checkbox enabled.
I checked through breakpoints. Its loading the previous gridpage values during page load event. And after pageloadevent its going design and loads the new values in grid.
//-------loading previous page values of grid here---------
protected void Page_Load(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
Label lbl = (Label)row.FindControl("Labely8");
Label Label23 = (Label)row.FindControl("Label23");
CheckBox checkbox = (CheckBox)row.FindControl("chkRows");
if (lbl.Text == "Validated")
{
checkbox.Enabled = false;
}
else
{
checkbox.Enabled = true;
}
}
}
I think you need to do enable or disable each of the checkboxes individually in the GridView.RowDataBound event rather than all at once in the Page_Load event:
void GridView1_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
CheckBox checkbox = (CheckBox)e.Row.FindControl("chkRows");
checkbox.Enabled = e.Row.Cells["nameOfCellWithLabel"].Text == "Validated";
}
}

Categories

Resources