Hi,
This is my code
using System; using System.Collections; using System.Configuration; using System.Data; using System.Web; using System.Web.Security; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI; using System.Data.SqlClient; public partial class AddItem : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { FirstGridViewRow(); Filldepartment(); } } private void FirstGridViewRow() { DataTable dt = new DataTable(); DataRow dr = null; dt.Columns.Add(new DataColumn("RowNumber", typeof(string))); dt.Columns.Add(new DataColumn("Col1", typeof(string))); dt.Columns.Add(new DataColumn("Col2", typeof(string))); dt.Columns.Add(new DataColumn("Col3", typeof(string))); dt.Columns.Add(new DataColumn("Col4", typeof(string))); dt.Columns.Add(new DataColumn("Col5", typeof(string))); dt.Columns.Add(new DataColumn("Col6", typeof(string))); dt.Columns.Add(new DataColumn("Col7", typeof(string))); dt.Columns.Add(new DataColumn("Col8", typeof(string))); dt.Columns.Add(new DataColumn("Col9", typeof(string))); dt.Columns.Add(new DataColumn("Col10", typeof(string))); dr = dt.NewRow(); dr["RowNumber"] = 1; dr["Col1"] = string.Empty; dr["Col2"] = string.Empty; dr["Col3"] = string.Empty; dr["Col4"] = string.Empty; dr["Col5"] = string.Empty; dr["Col6"] = string.Empty; dr["Col7"] = string.Empty; dr["Col8"] = string.Empty; dr["Col9"] = string.Empty; dr["Col10"] = string.Empty; dt.Rows.Add(dr); ViewState["CurrentTable"] = dt; grvStudentDetails.DataSource = dt; grvStudentDetails.DataBind(); TextBox txn = (TextBox)grvStudentDetails.Rows[0].Cells[1].FindControl("staffIDtxt"); txn.Focus(); Button btnAdd = (Button)grvStudentDetails.FooterRow.Cells[5].FindControl("ButtonAdd"); Page.Form.DefaultFocus = btnAdd.ClientID; } private void AddNewRow() { int rowIndex = 0; if (ViewState["CurrentTable"] != null) { DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"]; DataRow drCurrentRow = null; if (dtCurrentTable.Rows.Count > 0) { for (int i = 1; i <= dtCurrentTable.Rows.Count; i++) { TextBox col1 = (TextBox)grvStudentDetails.Rows[rowIndex].Cells[1].FindControl("staffIDtxt"); TextBox col2 = (TextBox)grvStudentDetails.Rows[rowIndex].Cells[2].FindControl("staffnametxt"); TextBox col3 = (TextBox)grvStudentDetails.Rows[rowIndex].Cells[3].FindControl("staffemailtxt"); TextBox col4 = (TextBox)grvStudentDetails.Rows[rowIndex].Cells[4].FindControl("fromdtext"); TextBox col5 = (TextBox)grvStudentDetails.Rows[rowIndex].Cells[5].FindControl("enddtext"); TextBox col6 = (TextBox)grvStudentDetails.Rows[rowIndex].Cells[6].FindControl("desctext"); DropDownList col7 = (DropDownList)grvStudentDetails.Rows[rowIndex].Cells[7].FindControl("projcodetext"); RadioButtonList col8 = (RadioButtonList)grvStudentDetails.Rows[rowIndex].Cells[8].FindControl("daytype"); TextBox col9 = (TextBox)grvStudentDetails.Rows[rowIndex].Cells[9].FindControl("totalhourstxt"); TextBox col10 = (TextBox)grvStudentDetails.Rows[rowIndex].Cells[10].FindControl("mealallowtxt"); drCurrentRow = dtCurrentTable.NewRow(); drCurrentRow["RowNumber"] = i + 1; dtCurrentTable.Rows[i - 1]["Col1"] = col1.Text; dtCurrentTable.Rows[i - 1]["Col2"] = col2.Text; dtCurrentTable.Rows[i - 1]["Col3"] = col3.Text; dtCurrentTable.Rows[i - 1]["Col4"] = col4.Text; dtCurrentTable.Rows[i - 1]["Col5"] = col5.Text; dtCurrentTable.Rows[i - 1]["Col6"] = col6.Text; dtCurrentTable.Rows[i - 1]["Col7"] = col7.SelectedValue; dtCurrentTable.Rows[i - 1]["Col8"] = col8.SelectedValue; dtCurrentTable.Rows[i - 1]["Col9"] = col9.Text; dtCurrentTable.Rows[i - 1]["Col10"] = col10.Text; rowIndex++; } dtCurrentTable.Rows.Add(drCurrentRow); ViewState["CurrentTable"] = dtCurrentTable; grvStudentDetails.DataSource = dtCurrentTable; grvStudentDetails.DataBind(); TextBox txn = (TextBox)grvStudentDetails.Rows[rowIndex].Cells[1].FindControl("staffIDtxt"); txn.Focus(); // txn.Focus; } } else { Response.Write("ViewState is null"); } SetPreviousData(); } private void SetPreviousData() { int rowIndex = 0; if (ViewState["CurrentTable"] != null) { DataTable dt = (DataTable)ViewState["CurrentTable"]; if (dt.Rows.Count > 0) { for (int i = 0; i < dt.Rows.Count; i++) { TextBox col1 = (TextBox)grvStudentDetails.Rows[rowIndex].Cells[1].FindControl("staffIDtxt"); TextBox col2 = (TextBox)grvStudentDetails.Rows[rowIndex].Cells[2].FindControl("staffnametxt"); TextBox col3 = (TextBox)grvStudentDetails.Rows[rowIndex].Cells[3].FindControl("staffemailtxt"); TextBox col4 = (TextBox)grvStudentDetails.Rows[rowIndex].Cells[4].FindControl("fromdtext"); TextBox col5 = (TextBox)grvStudentDetails.Rows[rowIndex].Cells[5].FindControl("enddtext"); TextBox col6 = (TextBox)grvStudentDetails.Rows[rowIndex].Cells[6].FindControl("desctext"); DropDownList col7 = (DropDownList)grvStudentDetails.Rows[rowIndex].Cells[7].FindControl("projcodetext"); RadioButtonList col8 = (RadioButtonList)grvStudentDetails.Rows[rowIndex].Cells[8].FindControl("daytype"); TextBox col9 = (TextBox)grvStudentDetails.Rows[rowIndex].Cells[9].FindControl("totalhourstxt"); TextBox col10 = (TextBox)grvStudentDetails.Rows[rowIndex].Cells[10].FindControl("mealallowtxt"); // drCurrentRow["RowNumber"] = i + 1; grvStudentDetails.Rows[i].Cells[0].Text = Convert.ToString(i + 1); col1.Text = dt.Rows[i]["Col1"].ToString(); col2.Text = dt.Rows[i]["Col2"].ToString(); col3.Text = dt.Rows[i]["Col3"].ToString(); col4.Text = dt.Rows[i]["Col4"].ToString(); col5.Text = dt.Rows[i]["Col5"].ToString(); col6.Text = dt.Rows[i]["Col6"].ToString(); col7.SelectedValue = dt.Rows[i]["Col7"].ToString(); col8.SelectedValue = dt.Rows[i]["Col8"].ToString(); col9.Text = dt.Rows[i]["Col9"].ToString(); col10.Text = dt.Rows[i]["Col10"].ToString(); rowIndex++; } } } } protected void ButtonAdd_Click(object sender, EventArgs e) { AddNewRow(); } protected void grvStudentDetails_RowDeleting(object sender, GridViewDeleteEventArgs e) { SetRowData(); if (ViewState["CurrentTable"] != null) { DataTable dt = (DataTable)ViewState["CurrentTable"]; DataRow drCurrentRow = null; int rowIndex = Convert.ToInt32(e.RowIndex); if (dt.Rows.Count > 1) { dt.Rows.Remove(dt.Rows[rowIndex]); drCurrentRow = dt.NewRow(); ViewState["CurrentTable"] = dt; grvStudentDetails.DataSource = dt; grvStudentDetails.DataBind(); for (int i = 0; i < grvStudentDetails.Rows.Count - 1; i++) { grvStudentDetails.Rows[i].Cells[0].Text = Convert.ToString(i + 1); } SetPreviousData(); } } } private void SetRowData() { int rowIndex = 0; if (ViewState["CurrentTable"] != null) { DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"]; DataRow drCurrentRow = null; if (dtCurrentTable.Rows.Count > 0) { for (int i = 1; i <= dtCurrentTable.Rows.Count; i++) { TextBox col1 = (TextBox)grvStudentDetails.Rows[rowIndex].Cells[1].FindControl("staffIDtxt"); TextBox col2 = (TextBox)grvStudentDetails.Rows[rowIndex].Cells[1].FindControl("staffnametxt"); TextBox col3 = (TextBox)grvStudentDetails.Rows[rowIndex].Cells[1].FindControl("staffemailtxt"); TextBox col4 = (TextBox)grvStudentDetails.Rows[rowIndex].Cells[1].FindControl("fromdtext"); TextBox col5 = (TextBox)grvStudentDetails.Rows[rowIndex].Cells[1].FindControl("enddtext"); TextBox col6 = (TextBox)grvStudentDetails.Rows[rowIndex].Cells[1].FindControl("desctext"); DropDownList col7 = (DropDownList)grvStudentDetails.Rows[rowIndex].Cells[1].FindControl("projcodetext"); ; RadioButtonList col8 = (RadioButtonList)grvStudentDetails.Rows[rowIndex].Cells[1].FindControl("daytype"); TextBox col9 = (TextBox)grvStudentDetails.Rows[rowIndex].Cells[1].FindControl("totalhourstxt"); TextBox col10 = (TextBox)grvStudentDetails.Rows[rowIndex].Cells[1].FindControl("mealallowtxt"); drCurrentRow = dtCurrentTable.NewRow(); drCurrentRow["RowNumber"] = i + 1; dtCurrentTable.Rows[i - 1]["Col1"] = col1.Text; dtCurrentTable.Rows[i - 1]["Col2"] = col2.Text; dtCurrentTable.Rows[i - 1]["Col3"] = col3.Text; dtCurrentTable.Rows[i - 1]["Col4"] = col4.Text; dtCurrentTable.Rows[i - 1]["Col5"] = col5.Text; dtCurrentTable.Rows[i - 1]["Col6"] = col6.Text; dtCurrentTable.Rows[i - 1]["Col7"] = col7.SelectedValue; dtCurrentTable.Rows[i - 1]["Col8"] = col8.SelectedValue; dtCurrentTable.Rows[i - 1]["Col9"] = col9.Text; dtCurrentTable.Rows[i - 1]["Col10"] = col10.Text; rowIndex++; } ViewState["CurrentTable"] = dtCurrentTable; //grvStudentDetails.DataSource = dtCurrentTable; //grvStudentDetails.DataBind(); } } else { Response.Write("ViewState is null"); } //SetPreviousData(); } protected void btnSave_Click(object sender, EventArgs e) { try { SetRowData(); DataTable table = ViewState["CurrentTable"] as DataTable; if (table != null) { foreach (DataRow row in table.Rows) { string staffIDtxt1 = row.ItemArray[1] as string; string staffnametxt2 = row.ItemArray[2] as string; string staffemailtxt3 = row.ItemArray[3] as string; string fromdtext4 = row.ItemArray[4] as string; string enddtext5 = row.ItemArray[5] as string; string desctext6 = row.ItemArray[6] as string; string projcodetext7 = row.ItemArray[7] as string; string daytype8 = row.ItemArray[8] as string; string totalhourstxt9 = row.ItemArray[9] as string; string mealallowtxt10 = row.ItemArray[10] as string; if (staffIDtxt1 != null || staffnametxt2 != null || staffemailtxt3 != null || fromdtext4 != null || enddtext5 != null || desctext6 != null || projcodetext7 != null || daytype8 != null || totalhourstxt9 != null || mealallowtxt10 != null) { // Do whatever is needed with this data, // Possibily push it in database // I am just printing on the page to demonstrate that it is working. Response.Write(string.Format("{0} {1} {2} {3} {4} {5} {6} {7} {8} {9} {10}<br/>", staffIDtxt1, staffnametxt2, staffemailtxt3, fromdtext4, enddtext5, desctext6, projcodetext7, daytype8, totalhourstxt9, mealallowtxt10)); } } } } catch (Exception ex) { throw new Exception(ex.Message); } } protected void grvStudentDetails_SelectedIndexChanged(object sender, EventArgs e) { } protected void endtime_TextChanged(object sender, EventArgs e) { GridViewRow currentRow =(GridViewRow)((TextBox)sender).Parent.Parent; TextBox txt1 = (TextBox)currentRow.FindControl("fromdtext"); TextBox txt2 = (TextBox)currentRow.FindControl("enddtext"); TextBox txt3 = (TextBox)currentRow.FindControl("totalhourstxt"); const string DateFormat = "dd/MM/yyyy hh:mm tt"; DateTime d1 = DateTime.ParseExact(txt1.Text, DateFormat, null); DateTime d2 = DateTime.ParseExact(txt2.Text, DateFormat, null); TimeSpan dateDiff = d2 - d1; txt3.Text = dateDiff.TotalHours.ToString("F"); } protected void fromtime_TextChanged(object sender, EventArgs e) { } private void Filldepartment() { SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["overtimeConnectionString"].ConnectionString); SqlCommand cmd = new SqlCommand("Select * from department", con); SqlDataAdapter da = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); da.Fill(ds); department1.DataTextField = ds.Tables[0].Columns["department_name"].ToString(); department1.DataValueField = ds.Tables[0].Columns["department_ID"].ToString(); department1.DataSource = ds.Tables[0]; department1.DataBind(); } protected void daytype_SelectedIndexChanged(object sender, EventArgs e) { GridViewRow currentRow = (GridViewRow)((RadioButtonList)sender).Parent.Parent; TextBox txt4 = (TextBox)currentRow.FindControl("mealallowtxt"); RadioButtonList txt5 = (RadioButtonList)currentRow.FindControl("daytype"); TextBox txt3 = (TextBox)currentRow.FindControl("totalhourstxt"); double hours = Convert.ToDouble(txt3.Text); int value = 0; if (txt5.SelectedValue == "N") { for (int i = 1; i <= hours; i++) { if (i % 3 == 0) { value = value + 20; } } txt4.Text = Convert.ToString(value); } else { for (int i = 1; i <= hours; i++) { if (i % 4 == 0) { value = value + 20; } } txt4.Text = Convert.ToString(value); } } }
i want to be able to call this function (protected void daytype_SelectedIndexChanged(object sender, EventArgs e)
))
during page load..on every postback..so that the value changes..
how do I do that