Now I'm going to explain how to change the Grid View Button Text or Image (Active/Inactive) based up on the database value bit column (True/False)
step2 : Binding Database table value in Data table
Private void FillGrid() // Filling values to Gridview
{
SqlConnection con = new SqlConnection(Constring);
con.Open();
SqlDataAdapter da = new SqlDataAdapter("select Userid,Isactive from Employee_Details, con);
\\Here Isactive was Bit Value
DataTable dt = new DataTable();
da.Fill(dt);
con.Close();
Grd.DataSource = dt;
Grd.DataBind();
foreach (GridViewRow row in Grd.Rows)
{
ImageButton btn1 = (ImageButton)(Grd.Rows[row.RowIndex].FindControl("btn1"));
ImageButton btn2 = (ImageButton)(Grd.Rows[row.RowIndex].FindControl("btn2"));
string Flag = dt.Rows[0]["Isactive"].ToString();
if (dt.Rows.Count > 0)
{
dt.Rows[0].Delete();
dt.AcceptChanges();
}
if (Flag == "True")
{
btn1.Visible = true;
btn2.Visible = false;
}
else
{
btn1.Visible = false;
btn2.Visible = true;
}
}
}
step1 : Add this Template field in your inside the Grid view column
step2 : Binding Database table value in Data table
Private void FillGrid() // Filling values to Gridview{
SqlConnection con = new SqlConnection(Constring);
con.Open();
SqlDataAdapter da = new SqlDataAdapter("select Userid,Isactive from Employee_Details, con);
\\Here Isactive was Bit Value
DataTable dt = new DataTable();
da.Fill(dt);
con.Close();
Grd.DataSource = dt;
Grd.DataBind();
foreach (GridViewRow row in Grd.Rows)
{
ImageButton btn1 = (ImageButton)(Grd.Rows[row.RowIndex].FindControl("btn1"));
ImageButton btn2 = (ImageButton)(Grd.Rows[row.RowIndex].FindControl("btn2"));
string Flag = dt.Rows[0]["Isactive"].ToString();
if (dt.Rows.Count > 0)
{
dt.Rows[0].Delete();
dt.AcceptChanges();
}
if (Flag == "True")
{
btn1.Visible = true;
btn2.Visible = false;
}
else
{
btn1.Visible = false;
btn2.Visible = true;
}
}
}
step3 : Binding Grid View
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
FillGrid(); \\Binding Grid View
}
{
if(!IsPostBack)
FillGrid(); \\Binding Grid View
}
step4 : Changing the Button Text (click the row command event in grid view )
protected void Grd_RowCommand(object sender, GridViewCommandEventArgs e)
{
string x = e.CommandName;//Getting the commenad name we mentioned in Gridview BUtton
if(x=="btn1"|x=="btn2")//checking the button click event which we mentioned in the bitton ie.. command argument
{
int cmd = Convert.ToInt32(e.CommandArgument);//Getting the command Argument ie.. which row we selecting
string id = Grd.Rows[cmd].Cells[0].Text;// Getting UserId from the Gridview selected row
SqlConnection con = new SqlConnection(Constring);
con.Open();
if (x == "btn1")
{
string Query = "update Employee_Details set Isactive = 0 where Userid=" + id;
SqlDataAdapter da = new SqlDataAdapter(Query, con);
}
else
{
string Query = "update Employee_Details set Isactive = 1 where Userid=" + id;
SqlDataAdapter da = new SqlDataAdapter(Query, con);
}
con.Close();
}
FillGrid();
}
{
string x = e.CommandName;//Getting the commenad name we mentioned in Gridview BUtton
if(x=="btn1"|x=="btn2")//checking the button click event which we mentioned in the bitton ie.. command argument
{
int cmd = Convert.ToInt32(e.CommandArgument);//Getting the command Argument ie.. which row we selecting
string id = Grd.Rows[cmd].Cells[0].Text;// Getting UserId from the Gridview selected row
SqlConnection con = new SqlConnection(Constring);
con.Open();
if (x == "btn1")
{
string Query = "update Employee_Details set Isactive = 0 where Userid=" + id;
SqlDataAdapter da = new SqlDataAdapter(Query, con);
}
else
{
string Query = "update Employee_Details set Isactive = 1 where Userid=" + id;
SqlDataAdapter da = new SqlDataAdapter(Query, con);
}
con.Close();
}
FillGrid();
}