vendredi 13 avril 2018

Move Checked CheckBox with gridview dynamic headers

I have a gridview with rowdatabound event that uses dynamic headers.

This is no problem and i works fine: ThisWeekMinus1().ToString(); = '201814' ThisWeekMinus2().ToString(); = '201813'

protected void gwPlanning_RowDataBound(object sender, GridViewRowEventArgs e)
{

    if (e.Row.Cells.Count > 0)
    {
        //Translate header text
        if (e.Row.RowType == DataControlRowType.Header)
        {

            e.Row.Cells[9].Text = ThisWeekMinus1().ToString();
            e.Row.Cells[10].Text = ThisWeekMinus2().ToString();
        }
    }
}

Now to the issue.

I have gridview columns with ItemTemplate that holds my Checkbox control

<asp:TemplateField HeaderText="This Week - 2">
    <EditItemTemplate>
        <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("[This Week - 2]") %>'></asp:TextBox>
    </EditItemTemplate>
    <ItemTemplate>
        <asp:CheckBox ID="CheckBox1" AutoPostBack="true"  OnCheckedChanged="myCheckBox_OnCheckedChange" runat="server" Style="text-align: center" />
    </ItemTemplate>
    <HeaderStyle Font-Bold="False" Font-Size="14px" />
    <ItemStyle Font-Size="12px" HorizontalAlign="Left" Width="30%" />
</asp:TemplateField>

The dynamic header named will change depending on the Year and Week we are in (ISO_WEEK)

I would like the checked checkbox to follow the dynamic header. So if i have checked 201813 and next week comes it should change to the next column (e.g. where ThisWeekMinus2().ToString()) is

enter image description here

My code behind for OncheckedChange event is:

protected void myCheckBox_OnCheckedChange(object sender, EventArgs e)
{

    CheckBox myCheckBox = (CheckBox)sender;
    GridViewRow row = (GridViewRow)myCheckBox.NamingContainer;

    string ThisWeekMinus2 = row.Cells[1].Text;
    bool status = myCheckBox.Checked;

    string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
    using (SqlConnection con = new SqlConnection(CS))
    {
        SqlCommand cmd = new SqlCommand("UPDATE [BI_Planning].[dbo].[tblPlanning] SET [This Week - 2] = @IsActive WHERE PlanningID = @ID  ", con);
        cmd.Parameters.Add("@IsActive", SqlDbType.Bit).Value = myCheckBox.Checked;
        cmd.Parameters.Add("@ID", SqlDbType.Int).Value = ThisWeekMinus2;
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();

        //gwPlanning.DataBind();

    }        

}




Aucun commentaire:

Enregistrer un commentaire