manish kedar

manish kedar

  • NA
  • 229
  • 11.5k

datatable and gridview

Dec 30 2017 3:14 AM
i get all gridview data in data table and i want to save the selected row in database using get xml method but all record of data table are saved in database. i have checbox in all gridview row and one button after grid. 
 
i show the regarding code:
 
 
 
 
 
 
on button  cilck 
 
private DataTable CreateDataTable()
{
// dtable.Reset();
DataTable dtable = new DataTable();
dtable = (DataTable)ViewState["dsStudent"];
dtable.Columns.Add("InTime");
dtable.Columns.Add("OutTime");
dtable.Columns.Add("LeaveType");
dtable.Columns.Add("Remarks");
return dtable;
}
 
 
 
 
 
protected void btnAttendanceSave_Click(object sender, EventArgs e)
{
try
{
SetProperties();
DataTable dtable = CreateDataTable();
for (int i = 0; i < gvStudentAttendance.Rows.Count; i++)
{
TextBox txtInTime = (TextBox)gvStudentAttendance.Rows[i].Cells[2].FindControl("txtAttInTime");
TextBox txtOutTime = (TextBox)gvStudentAttendance.Rows[i].Cells[3].FindControl("txtAttOutTime");
DropDownList LeaveType = (DropDownList)gvStudentAttendance.Rows[i].Cells[4].FindControl("ddlAttLeaveType");
TextBox txtRemarks = (TextBox)gvStudentAttendance.Rows[i].Cells[5].FindControl("txtAttRemarks");
dtable.Rows[i]["InTime"] = txtInTime.Text;
dtable.Rows[i]["OutTime"] = (txtOutTime.Text);
dtable.Rows[i]["LeaveType"] = LeaveType.SelectedValue;
dtable.Rows[i]["Remarks"] = txtRemarks.Text;
gvStudentAttendance.DataSource = null;
gvStudentAttendance.DataBind();
}
DataSet ds = new DataSet();
ds.Tables.Add(dtable);
ds.DataSetName = "dsStudent";
ds.Tables[0].TableName = "AttendanceTable";
//ds.Tables[0].Columns.Add();
string _error;
if (cls_StudentAttendance_Obj.DML(2, ds, out _error) > 0)
{
GlobalMethods.AlertMsg(divError, divSuccess, "Attendance taken successfully.", 1);
Methods.ClearInputs(Page.Controls);
dtable.Columns.Remove("InTime");
dtable.Columns.Remove("OutTime");
dtable.Columns.Remove("LeaveType");
dtable.Columns.Remove("Remarks");
}
else
{
GlobalMethods.AlertMsg(divError, divSuccess, "Attendance not taken.", 0);
dtable.Columns.Remove("InTime");
dtable.Columns.Remove("OutTime");
dtable.Columns.Remove("LeaveType");
dtable.Columns.Remove("Remarks");
}
}
catch (Exception ex)
{
throw;
}
}

Answers (2)