i want to make group in grid view as ID for update with help of sql server.
but not create.
my code is.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
gvbind();
}
}
protected void gvbind()
{
con.Open();
SqlCommand cmd = new SqlCommand("Select * from shopDetails1", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
if (ds.Tables[0].Rows.Count > 0)
{
GridView1.DataSource = ds;
GridView1.DataBind();
ShowGrpDataInGridView(GridView1.Rows, 0, 18);
}
else
{
ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
GridView1.DataSource = ds;
GridView1.DataBind();
int columncount = GridView1.Rows[0].Cells.Count;
GridView1.Rows[0].Cells.Clear();
GridView1.Rows[0].Cells.Add(new TableCell());
GridView1.Rows[0].Cells[0].ColumnSpan = columncount;
GridView1.Rows[0].Cells[0].Text = "No Records Found";
}
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
Label lblID = (Label)row.FindControl("lblID");
// TextBox txtname=(TextBox)row.cell[].control[0];
TextBox txtstore = (TextBox)row.Cells[1].Controls[0];
TextBox txtmobile = (TextBox)row.Cells[2].Controls[0];
TextBox txtD_form_type = (TextBox)row.Cells[3].Controls[0];
TextBox txtD_Lic_no = (TextBox)row.Cells[4].Controls[0];
TextBox txtD_validfrom = (TextBox)row.Cells[5].Controls[0];
TextBox txtD_validupto = (TextBox)row.Cells[6].Controls[0];
TextBox txtL_validfrom = (TextBox)row.Cells[7].Controls[0];
TextBox txtL_validUpto = (TextBox)row.Cells[8].Controls[0];
TextBox txtS_validfrom = (TextBox)row.Cells[9].Controls[0];
TextBox txtS_validupto = (TextBox)row.Cells[10].Controls[0];
TextBox txtHealthreg = (TextBox)row.Cells[11].Controls[0];
TextBox txtH_validfrom = (TextBox)row.Cells[12].Controls[0];
TextBox txtH_validupto = (TextBox)row.Cells[13].Controls[0];
TextBox txtFoodreg = (TextBox)row.Cells[14].Controls[0];
TextBox txtF_validfrom = (TextBox)row.Cells[15].Controls[0];
TextBox txtF_validupto = (TextBox)row.Cells[16].Controls[0];
GridView1.EditIndex = -1;
con.Open();
//SqlCommand cmd = new SqlCommand("SELECT * FROM shopDetails1", conn);
SqlCommand cmd = new SqlCommand("update shopDetails1 set store='" + txtstore.Text + "',mobile='" + txtmobile.Text + "',D_form_type='" + txtD_form_type.Text + "',D_Lic_no='" + txtD_Lic_no.Text + "',D_validupto='" + txtD_validupto.Text + "',L_validfrom='" + txtL_validfrom.Text + "',L_validUpto='" + txtL_validUpto.Text + "',S_validfrom='" + txtS_validfrom.Text + "',S_validupto='" + txtS_validupto + "',Healthreg ='" + txtHealthreg + "',H_validfrom='" + txtH_validfrom + "',H_validupto='" + txtH_validupto + "',Foodreg='" + txtFoodreg + "',F_Validfrom='" + txtF_validfrom + "',F_validupto='" + txtF_validupto + "' where id='" + Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString()) + "'", con);
cmd.ExecuteNonQuery();
con.Close();
gvbind();
}
void ShowGrpDataInGridView(GridViewRowCollection gridViewRows, int startIndex, int totalColumns)
{
if (totalColumns == 0) return;
// int id = Convert.ToInt32(GridView1.DataKeys[].Value.ToString());
int i, count = 0;
ArrayList ast = new ArrayList();
ast.Add(gridViewRows[0]);
var ctrl = gridViewRows[0].Cells[startIndex];
for (i = 1; i < gridViewRows.Count; i++)
{
TableCell nextTbCell = gridViewRows[i].Cells[startIndex];
if (ctrl.Text == nextTbCell.Text)
{
count++;
nextTbCell.Visible = false;
ast.Add(gridViewRows[i]);
}
else
{
if (count > 0)
{
ctrl.RowSpan = count;
ShowGrpDataInGridView(new GridViewRowCollection(ast), startIndex + 1, totalColumns - 1);
}
count = 0;
ast.Clear();
ctrl = gridViewRows[i].Cells[startIndex];
ast.Add(gridViewRows[i]);
}
}
if (count > 0)
{
ctrl.RowSpan = count;
ShowGrpDataInGridView(new GridViewRowCollection(ast), startIndex + 1, totalColumns - 1);
}
count = 0;
ast.Clear();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
Label lbldeleteid = (Label)row.FindControl("lblID");
con.Open();
SqlCommand cmd = new SqlCommand("Delete FROM shopDetails1 where id='" + Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString()) + "'", con);
cmd.ExecuteNonQuery();
con.Close();
gvbind();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
gvbind();
}
i want this type of grouping but not do ..please help me
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Ramesh PalanivelPosted Mar 24, 2017, 4:46 AM