On button click add a row in datatable
protected void btnadd_Click(object sender, EventArgs e)
{
if (DDLdoctype.SelectedIndex != 0)
{
gv_DocParaType.DataSource = GetCustomMadeDataTable(1);
}
if (gv_DocParaType.Rows.Count >= 1)
{
foreach (GridViewRow gvr in gv_DocParaType.Rows)
{
gv_DocParaType.UpdateRow(gvr.RowIndex,false);
}
}
gv_DocParaType.DataBind();
if (gv_DocParaType.Rows.Count >= 1)
{
foreach (GridViewRow gvr in gv_DocParaType.Rows)
{
((DropDownList)gvr.Cells[2].FindControl("DDLParaType")).Enabled = true;
((DropDownList)gvr.Cells[1].FindControl("DDLPara")).Enabled = true;
((TextBox)gvr.Cells[3].FindControl("txtweightage")).Enabled = true;
}
}
}
this is required as need to update the content of previously added row
protected void gv_DocParaType_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
//Retrieve the table from the session object.
DataSet dt = (DataSet)Session["ds"];
//Update the values.
GridViewRow row = gv_DocParaType.Rows[e.RowIndex];
dt.Tables[0].Rows[row.DataItemIndex][1] = ((DropDownList)row.Cells[2].FindControl("DDLPara")).SelectedItem.Text;
dt.Tables[0].Rows[row.DataItemIndex][3] = ((DropDownList)row.Cells[2].FindControl("DDLParaType")).SelectedItem.Text;
dt.Tables[0].Rows[row.DataItemIndex][4] = ((TextBox)row.Cells[3].FindControl("txtweightage")).Text;
Session["ds"] = dt;
}
Assign the value of the datatable present in database
protected void gv_DocParaType_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowIndex != -1)
{
DropDownList ddl = e.Row.Cells[2].FindControl("DDLParaType") as DropDownList;
DropDownList ddlpara = e.Row.Cells[1].FindControl("DDLPara") as DropDownList;
TextBox txt = e.Row.Cells[3].FindControl("txtweightage") as TextBox;
BindDDLParaType(ddl);
BindDDLPara(ddlpara);
if (ddl.Items.Count > 1)
{
if (((System.Data.DataRowView)(e.Row.DataItem)).Row.ItemArray[1].ToString() != "")
{
ddlpara.Items.FindByText(((System.Data.DataRowView)(e.Row.DataItem)).Row.ItemArray[1].ToString()).Selected = true;
}
if (((System.Data.DataRowView)(e.Row.DataItem)).Row.ItemArray[3].ToString() != "")
{
ddl.Items.FindByText(((System.Data.DataRowView)(e.Row.DataItem)).Row.ItemArray[3].ToString()).Selected = true;
}
}
ddl.Enabled = false;
txt.Enabled = false;
ddlpara.Enabled = false;
}
}
bind the gridview
private void BindFormView()
{
if (DDLdoctype.SelectedIndex != 0)
{
gv_DocParaType.DataSource = GetCustomMadeDataTable();
}
gv_DocParaType.DataBind();
}
Get the datatable
private DataSet GetCustomMadeDataTable()
{
if (Session["ds"] == null)
{
SQL = new string[1, 2];
DataSet dsParameters = new DataSet("PMS_DOC");
SQL[0, 0] = "select da.parameter_doc_association_id id,p.parameter,d.pms_doc_type,pe.parameter_type,da.weightage from PMS_PARAM_DOC_ASSOC da,pms_parameter p,pms_doc d,pms_pe_parametertype pe where da.parameter_id=p.parameter_id and da.parameter_type_id=pe.parameter_type_id and da.pms_doc_id=d.pms_doc_id and da.pms_doc_id=" + DDLdoctype.SelectedValue.ToString() + " order by pe.parameter_type";
SQL[0, 1] = "PMS_DOC";
dsParameters = dao.GetDataset(SQL);
return dsParameters;
}
else return (DataSet)Session["ds"];
}
private DataSet GetCustomMadeDataTable(int x)
{
DataSet dsParameters = new DataSet("PMS_DOC");
if (Session["ds"] == null)
{
SQL = new string[1, 2];
SQL[0, 0] = "select da.parameter_doc_association_id id,p.parameter,d.pms_doc_type,pe.parameter_type,da.weightage from PMS_PARAM_DOC_ASSOC da,pms_parameter p,pms_doc d,pms_pe_parametertype pe where da.parameter_id=p.parameter_id and da.parameter_type_id=pe.parameter_type_id and da.pms_doc_id=d.pms_doc_id and da.pms_doc_id=" + DDLdoctype.SelectedValue.ToString() + " order by pe.parameter_type";
SQL[0, 1] = "PMS_DOC";
dsParameters = dao.GetDataset(SQL);
}
else
{
dsParameters = (DataSet)Session["ds"];
}
dsParameters.Tables[0].Rows.Add(dsParameters.Tables[0].NewRow());
Session["ds"] = dsParameters;
return dsParameters;
}