Click here to Skip to main content
15,896,915 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello

I have one grid view which is dynamically created.Now I am trying to insert the row values in database using foreach (GridViewRow gvrow in GrdDynamic.Rows) but it shows row count 0.But when I set the autogeneratecolumns property true it gives row count and column count accurate but while fetching records the values from textbox controls gives null why is this happening...?
Posted
Comments
Dilan Shaminda 19-Jul-14 1:16am    
Have you set DataPropertyName for your columns in the gridview?
nilesh sawardekar 19-Jul-14 1:27am    
can you post your code snipset..there may be some problem..looks like..
vrushali arbune 19-Jul-14 1:53am    
private void BindGrid()
{
try
{
dsEmp = emp.GetEmployee(customerId, ddlEmpType.SelectedValue);
dsAdd = new DataSet();
dsAdd = salGen.GetAdditionComponent(customerId);
if (dsAdd != null && dsAdd.Tables[0].Rows.Count > 0)
{
dtAddNew = new DataTable();
dtAdd = new DataTable();
foreach (DataRow dr in dsAdd.Tables[0].Rows)
{
DataColumn dc = new DataColumn();
dc.ColumnName = dr["SalComponentName"].ToString();
dtAddNew.Columns.Add(dc);
addColCount++;
}
dtAdd = dtAddNew;
}
dsDed = new DataSet();

dsDed = salGen.GetDeductionComponent(customerId);
if (dsDed != null && dsDed.Tables[0].Rows.Count > 0)
{
dtDedNew = new DataTable();
dtDed = new DataTable();
foreach (DataRow dr in dsDed.Tables[0].Rows)
{
DataColumn dc = new DataColumn();
dc.ColumnName = dr["SalComponentName"].ToString();
dtDedNew.Columns.Add(dc);
dedColCount++;
}
DataColumn dcLast = new DataColumn();
dcLast.ColumnName = "Total";
dtDedNew.Columns.Add(dcLast);
dedColCount++;
hfLastColCount.Value = dedColCount.ToString();
dtDed = dtDedNew;
}
dt = new DataTable();
dt = dsEmp.Tables[0];
dt.Merge(dtAddNew);
dt.Merge(dtDedNew);
//Iterate through the columns of the datatable to set the data bound field dynamically.
int i = 0;

foreach (DataColumn col in dt.Columns)
{

//Declare the bound field and allocate memory for the bound field.
TemplateField bfield = new TemplateField();


//Initalize the DataField value.
bfield.HeaderTemplate = new GridViewTemplate(ListItemType.Header, col.ColumnName);
if (i < 2)
{
//Initialize the HeaderText field value.
bfield.ItemTemplate = new GridViewTemplate(ListItemType.Item, col.ColumnName,"l"+i);
//Initialize the HeaderText field value.
}
else
{
bfield.ItemTemplate = new GridViewTemplate(ListItemType.EditItem, col.ColumnName, "t" + i);
}
//Add the newly created bound field to the GridView.
GrdDynamic.Columns.Add(bfield);
i++;
}

//Initialize the DataSource
GrdDynamic.DataSource = dt;

//Bind the datatable with the GridView.
GrdDynamic.DataBind();
addColCount = 0;
dedColCount = 0;

}
catch (Exception)
{

throw;
}
}
vrushali arbune 19-Jul-14 1:56am    
protected void btnSubmit_Click(object sender, EventArgs e)
{
try
{
btn = (Button)sender;
btnnm = btn.Text;
if (btnnm == "Submit")
{
if (ddlEmpType.SelectedValue != null && txtDate.Text != "")
{
BindGrid();

}
ActiveControls(btnnm);
}
else
{
int i = 0;
int k = 2;
int c = GrdDynamic.Rows.Count;

foreach (GridViewRow gvrow in GrdDynamic.Rows)
{



int counter = 1;



int NoOfColumns = GrdDynamic.Rows[0].Cells.Count;
lblEmpName = (Label)gvrow.FindControl("t"+k);
lblEmpId = (Label)gvrow.Cells[1].FindControl("l1");
string nm= GrdDynamic.Rows[0].Cells[0].Text;
// txtTotal = (TextBox)GrdDynamic.Rows[0].Cells[0].Controls[0];
for (int j = 2; j <= NoOfColumns - 2; j++)
{
string colName = GrdDynamic.HeaderRow.Cells[j].Text;
getSalCompIDType(colName);
txtSal = (TextBox)gvrow.Cells[j].FindControl("t"+j);
//salGen = new Salary_Generation();
salGen.customer_Id = customerId;
salGen.date = Convert.ToDateTime(txtDate.Text);
salGen.Emp_id =Convert.ToInt64( lblEmpId.Text);
salGen.Sal_cat_type = type;
salGen.sal_toatal = Convert.ToDecimal(txtTotal.Text);
salGen.Salary = Convert.ToDecimal(txtSal.Text);
salGen.user_Id = userId;
salGen.Salary_Comp_Id = id;

string res = salGen.InsertSalaryGeneration(salGen);


}
k++;
}
}
}
catch (Exception)
{

throw;
}
}
Dilan Shaminda 19-Jul-14 2:19am    
please update your question with the code snippet.So it will be easy to read and understand.

1 solution

That is because on each post back GridView becomes empty. So, what you have to do is on each post back you have to again add the controls to the Grid and assign the cell values to the controls as well. Then you can actually loop through the controls to get their values.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900