2014年7月13日 星期日

[RESOLVED] Grid View Update


Dear Friends,


                 I am inserting data from grid using button control. Hence there is no Button controls inside the grid.


When loading the grid at first time initially first row will be there and if i click the "Add" button means rows will create. For this my code part is,


#region SetInitialRowforTransfer

///



/// SetInitialRowforTransfer

///


private void SetInitialRowforTransfer()

{

try

{

DataTable dt = new DataTable();


DataRow dr = null;


dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));


dt.Columns.Add(new DataColumn("Column1", typeof(string)));


dt.Columns.Add(new DataColumn("Column2", typeof(string)));


dt.Columns.Add(new DataColumn("Column3", typeof(string)));


dt.Columns.Add(new DataColumn("Column4", typeof(string)));


dt.Columns.Add(new DataColumn("Column5", typeof(string)));


dt.Columns.Add(new DataColumn("Column6", typeof(string)));


dt.Columns.Add(new DataColumn("Column7", typeof(string)));


dr = dt.NewRow();


dr["RowNumber"] = 1;


dr["Column1"] = string.Empty;


dr["Column2"] = string.Empty;


dr["Column3"] = string.Empty;


dr["Column4"] = string.Empty;


dr["Column5"] = string.Empty;


dr["Column6"] = string.Empty;


dr["Column7"] = string.Empty;


dt.Rows.Add(dr);


//dr = dt.NewRow();



//Store the DataTable in ViewState


ViewState["CurrentTableforTransfer"] = dt;



gvContact.DataSource = dt;


gvContact.DataBind();

}

catch (Exception ex)

{


throw ex;

}




}

#endregion


#region AddNewRowToGridforTransfer

///



/// AddNewRowToGridforTransfer

///


private void AddNewRowToGridforTransfer()

{

try

{


int rowIndex = 0;



if (ViewState["CurrentTableforTransfer"] != null)

{


DataTable dtCurrentTable = (DataTable)ViewState["CurrentTableforTransfer"];


DataRow drCurrentRow = null;


if (dtCurrentTable.Rows.Count > 0)

{


for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)

{


//extract the TextBox values


TextBox box1 = (TextBox)gvContact.Rows[rowIndex].Cells[1].FindControl("txtTransferPLaceFrom");


TextBox box2 = (TextBox)gvContact.Rows[rowIndex].Cells[2].FindControl("txtDesignTransfer");


TextBox box3 = (TextBox)gvContact.Rows[rowIndex].Cells[3].FindControl("txtTansferDate");


TextBox box4 = (TextBox)gvContact.Rows[rowIndex].Cells[4].FindControl("txtToPlace");


TextBox box5 = (TextBox)gvContact.Rows[rowIndex].Cells[5].FindControl("txtJoiningDate");


TextBox box6 = (TextBox)gvContact.Rows[rowIndex].Cells[6].FindControl("txtTransferReason");


TextBox box7 = (TextBox)gvContact.Rows[rowIndex].Cells[7].FindControl("txtTransferemail");



drCurrentRow = dtCurrentTable.NewRow();


drCurrentRow["RowNumber"] = i + 1;



dtCurrentTable.Rows[i - 1]["Column1"] = box1.Text;


dtCurrentTable.Rows[i - 1]["Column2"] = box2.Text;


dtCurrentTable.Rows[i - 1]["Column3"] = box3.Text;


dtCurrentTable.Rows[i - 1]["Column4"] = box4.Text;


dtCurrentTable.Rows[i - 1]["Column5"] = box5.Text;


dtCurrentTable.Rows[i - 1]["Column6"] = box6.Text;


dtCurrentTable.Rows[i - 1]["Column7"] = box7.Text;



rowIndex++;


}


dtCurrentTable.Rows.Add(drCurrentRow);


ViewState["CurrentTableforTransfer"] = dtCurrentTable;



gvContact.DataSource = dtCurrentTable;


gvContact.DataBind();


}


}


else

{


Response.Write("ViewState is null");


}



//Set Previous Data on Postbacks


SetPreviousDataforTransfer();

}

catch (Exception ex)

{


throw ex;

}


}

#endregion


#region SetPreviousDataforTransfer

///



/// SetPreviousDataforTransfer

///


private void SetPreviousDataforTransfer()

{


try

{

int rowIndex = 0;


if (ViewState["CurrentTableforTransfer"] != null)

{


DataTable dt = (DataTable)ViewState["CurrentTableforTransfer"];


if (dt.Rows.Count > 0)

{


for (int i = 0; i < dt.Rows.Count; i++)

{


TextBox box1 = (TextBox)gvContact.Rows[rowIndex].Cells[1].FindControl("txtTransferPLaceFrom");


TextBox box2 = (TextBox)gvContact.Rows[rowIndex].Cells[2].FindControl("txtDesignTransfer");


TextBox box3 = (TextBox)gvContact.Rows[rowIndex].Cells[3].FindControl("txtTansferDate");


TextBox box4 = (TextBox)gvContact.Rows[rowIndex].Cells[4].FindControl("txtToPlace");


TextBox box5 = (TextBox)gvContact.Rows[rowIndex].Cells[5].FindControl("txtJoiningDate");


TextBox box6 = (TextBox)gvContact.Rows[rowIndex].Cells[6].FindControl("txtTransferReason");


TextBox box7 = (TextBox)gvContact.Rows[rowIndex].Cells[7].FindControl("txtTransferemail");




box1.Text = dt.Rows[i]["Column1"].ToString();


box2.Text = dt.Rows[i]["Column2"].ToString();


box3.Text = dt.Rows[i]["Column3"].ToString();


box4.Text = dt.Rows[i]["Column4"].ToString();


box5.Text = dt.Rows[i]["Column5"].ToString();


box6.Text = dt.Rows[i]["Column6"].ToString();


box7.Text = dt.Rows[i]["Column7"].ToString();



rowIndex++;


}


}


}


}

catch (Exception ex)

{


throw ex;

}

}

#endregion


#region ButtonAddfoTransfer_Click

///



/// ButtonAddfoTransfer_Click

///


///

///

protected void ButtonAddfoTransfer_Click(object sender, EventArgs e)

{


AddNewRowToGridforTransfer();


}

#endregion



Then i can insert the data by using following Insert code,


#region InsertGridTransferDetails


public void InsertGridTransferDetails(int sid)

{

BO_ObjSupplierMaster = new BO_Supplier_Master();

BE_ObjSupplierContact = new BE_Supplier_Contact();

try

{


foreach (GridViewRow row in gvContact.Rows)

{


TextBox txtplacefrm = (TextBox)row.FindControl("txtTransferPLaceFrom");

TextBox txtdesigntransfer = (TextBox)row.FindControl("txtDesignTransfer");

TextBox txttransferdate = (TextBox)row.FindControl("txtTansferDate");

TextBox txttoplace = (TextBox)row.FindControl("txtToPlace");

TextBox txtjoindate = (TextBox)row.FindControl("txtJoiningDate");

TextBox txttransferreason = (TextBox)row.FindControl("txtTransferReason");

TextBox txttransferemail = (TextBox)row.FindControl("txtTransferemail");


BE_ObjSupplierContact.Sup_Con_Lname1 = txtplacefrm.Text;

BE_ObjSupplierContact.Sup_Con_Mname1 = txtdesigntransfer.Text;

BE_ObjSupplierContact.Sup_Con_Fname1 = txttransferdate.Text;


BE_ObjSupplierContact.Sup_Con_Title1 = txttoplace.Text;

BE_ObjSupplierContact.Sup_Con_Telephone1 = txtjoindate.Text;

BE_ObjSupplierContact.Sup_Con_Mob1 = txttransferreason.Text;

BE_ObjSupplierContact.Sup_Con_Email1 = txttransferemail.Text;


BE_ObjSupplierContact.Sup_Master_Key1 = Convert.ToInt16(sid);


BO_ObjSupplierMaster.InsertSupplierContact(BE_ObjSupplierContact);


}





}

catch (Exception ex)

{


throw ex;

}

}

#endregion



Front end code is,



BorderColor="#A01E1E" Width="85%" HeaderStyle-Height="40px" RowStyle-Height="30px"

RowStyle-BorderColor="#A01E1E" runat="server" ShowFooter="true" AutoGenerateColumns="false">
















































































Text="Add" />









Now i want to Update the record. How can i bind the data to the grid. Please guide me to bind the data.



hey, once you add a new row to datatable, you can rebind the datatable to gridview again as


gvContact.DataSource = dtCurrentTable;


gvContact.DataBind();



and it will automatically update record



Hi,


Goto following link it has shown insert,update and delete inline.


http://www.codeproject.com/Articles/24085/Insert-Update-Delete-with-Gridview-Simple-Way


沒有留言:

張貼留言