get row data value& datatable filter & new datatable from datatable

来源:互联网 发布:盛科网络上市没 编辑:程序博客网 时间:2024/06/08 13:38

1. RowDataBound

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

{
if (e.Row.RowType == DataControlRowType.DataRow)
{

DefaultValue = ((GridView)sender).DataKeys[e.Row.RowIndex].Value.ToString();

}

}


2.  Checkbox CheckedChanged Event


  protected void ckbBillable_CheckedChanged(object sender, EventArgs e)
        {
            GridViewRow row = ((GridViewRow)((CheckBox)sender).NamingContainer);
            CheckBox ckbBillable = (CheckBox)row.FindControl("ckbBillable");
            Guid accountId = new Guid(gvAccountBillable.DataKeys[row.RowIndex]["account_id"].ToString());

          // Filter in datatable
            DataRow[] rows = dtAccountBillable.Select("account_id='" + accountId + "'");

            //Update in View state Data table
            if (rows.Length > 0)
            {
                rows[0]["is_billable"] = ckbBillable.Checked;
            }

            //if checkbox is checd, change the row color
            if (ckbBillable.Checked)
            {
                row.CssClass = "SelectRowStyle";
            }
            else
            {
                row.CssClass = "";
            }
        }


3. in the button click event

   protected void btnSubmit_Click(object sender, EventArgs e)
  {
            Guid rootAccountID = new Guid(gvAccount.DataKeys[0]["root_account_id"].ToString());

             DataView dvAccountBilllingInfo = dtAccountBillable.DefaultView;

          //  new table just has id and is_billable columns.  These two field values are from datatable dtAccountBillable.
            DataTable dtAccountBillingUpdate = dvAccountBilllingInfo.ToTable(true, " id", "is_billable");

}