Single OnRowDataBound event for multiple GridViews using C# in ASP.Net

kankon
 
on Sep 30, 2021 04:43 AM
615 Views

hello,

if i have 3 gridview same html page

can i use same OnRowDeleting and OnRowDataBound

i did add first one OnRowDeleting and OnRowDataBound working 

can add also for second and third OnRowDeleting and OnRowDataBound because when i try add am getting error

protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
    //rasmi
    if (e.Row.RowType == DataControlRowType.DataRow && e.Row.RowIndex != GridView2.EditIndex)
    {
        string namerm = (e.Row.FindControl("txtUsernamelb0") as Label).Text;
        string daterm = Convert.ToDateTime((e.Row.FindControl("lblBoxDateadd3") as Label).Text).ToString("dd/MM/yyyy");
 
        foreach (LinkButton button in e.Row.Cells[7].Controls.OfType<LinkButton>())
        {
 
            if (button.CommandName == "Delete")
            {
                button.Attributes["onclick"] = "if(!confirm('Do you want to delet\\nأسم الموظف\\n"
                    + namerm + "\\nتاريخ المهمة الرسمية\\n" + daterm + "')){ return false; };";
            }
        }
    }
        //EME
        if (e.Row.RowType == DataControlRowType.DataRow && e.Row.RowIndex != GridView3.EditIndex)
        {
            string nameeme = (e.Row.FindControl("txtUsernamelb1") as Label).Text;
            string dateeme = Convert.ToDateTime((e.Row.FindControl("lblBoxDateadd5") as Label).Text).ToString("dd/MM/yyyy");
 
            foreach (LinkButton button in e.Row.Cells[6].Controls.OfType<LinkButton>())
            {
 
                if (button.CommandName == "Delete")
                {
                    button.Attributes["onclick"] = "if(!confirm('Do you want to delet\\nأسم الموظف\\n"
                        + nameeme + "\\nتاريخ الاجازة الطارئة\\n" + dateeme + "')){ return false; };";
                }
            }
        }
}
  
protected void OnRowDeleting(object sender, GridViewDeleteEventArgs e)
{
    int index = Convert.ToInt32(e.RowIndex);
    int Id = Convert.ToInt32(GridView2.DataKeys[e.RowIndex].Values[0]);
    string constr = ConfigurationManager.ConnectionStrings["kankonConnectionString"].ConnectionString;
    using (SqlConnection con = new SqlConnection(constr))
    {
        using (SqlCommand cmd = new SqlCommand("Rsmi"))
        {
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@Action", "DELETE");
            cmd.Parameters.AddWithValue("@Id", Id);
            cmd.Connection = con;
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
        }
        BindGrid2Rasmisearch();
    }
 
    int index2 = Convert.ToInt32(e.RowIndex);
    int Id2 = Convert.ToInt32(GridView3.DataKeys[e.RowIndex].Values[0]);
    string constr2 = ConfigurationManager.ConnectionStrings["kankonConnectionString"].ConnectionString;
    using (SqlConnection con = new SqlConnection(constr2))
    {
        using (SqlCommand cmd = new SqlCommand("EME"))
        {
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@Action", "DELETE");
            cmd.Parameters.AddWithValue("@Id", Id2);
            cmd.Connection = con;
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
        }
        BindGrid3EMEsearch();
    } 
}

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
kankon
 
on Sep 30, 2021 10:55 AM
on Sep 30, 2021 06:34 PM

i found out i must change name OnRowDataBound with each gridview to 

OnRowDataBound="GridView1_RowDataBound"

OnRowDataBound="GridView2_RowDataBound"

OnRowDataBound="GridView3_RowDataBound"

then from code behind c#

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    //sp 
    if (e.Row.RowType == DataControlRowType.DataRow && e.Row.RowIndex != GridView1.EditIndex)
    {
        string namesp = (e.Row.FindControl("txtUsernamelb") as Label).Text;
        string datesp2 = Convert.ToDateTime((e.Row.FindControl("lblBoxDateadd") as Label).Text).ToString("dd/MM/yyyy");
 
        foreach (LinkButton button in e.Row.Cells[6].Controls.OfType<LinkButton>())
        {
 
            if (button.CommandName == "Delete")
            {
                button.Attributes["onclick"] = "if(!confirm('Do you want to delet\\nأسم الموظف\\n"
                    + namesp + "\\nتاريخ المهمة \\n" + datesp2 + "')){ return false; };";
            }
        }
    }
}
protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
{
    //rasmi
    if (e.Row.RowType == DataControlRowType.DataRow && e.Row.RowIndex != GridView2.EditIndex)
    {
        string namerm = (e.Row.FindControl("txtUsernamelb0") as Label).Text;
        string daterm = Convert.ToDateTime((e.Row.FindControl("lblBoxDateadd3") as Label).Text).ToString("dd/MM/yyyy");
 
        foreach (LinkButton button in e.Row.Cells[7].Controls.OfType<LinkButton>())
        {
 
            if (button.CommandName == "Delete")
            {
                button.Attributes["onclick"] = "if(!confirm('Do you want to delet\\nأسم الموظف\\n"
                    + namerm + "\\nتاريخ المهمة الرسمية\\n" + daterm + "')){ return false; };";
 
            }
        }
    }
}
protected void GridView3_RowDataBound(object sender, GridViewRowEventArgs e)
{
    //EME
    if (e.Row.RowType == DataControlRowType.DataRow && e.Row.RowIndex != GridView3.EditIndex)
    {
        string nameeme = (e.Row.FindControl("txtUsernamelb1") as Label).Text;
        string dateeme = Convert.ToDateTime((e.Row.FindControl("lblBoxDateadd5") as Label).Text).ToString("dd/MM/yyyy");
 
        foreach (LinkButton button in e.Row.Cells[5].Controls.OfType<LinkButton>())
        {
 
            if (button.CommandName == "Delete")
            {
                button.Attributes["onclick"] = "if(!confirm('Do you want to delet\\nأسم الموظف\\n"
                    + nameeme + "\\nتاريخ الاجازة الطارئة\\n" + dateeme + "')){ return false; };";
            }
        }
    }
}