C# .NET - inserting a new row in gridview view control in web form

Asked By muthuraman alexander on 12-Sep-11 05:11 AM
HI All
in my web page i have gridView control in which data is already bounded

Now i want to insert a new any index(ie before any row ,what i want )

i have created the gridview in .ASPx and binded DATA in it also it showing records taken from the DB
i created the item template with image button to create the new row and to delete the existing the rows

like the below picture

S.No

Items

Quantity

Credit

Debit

Delivered

 

1

Apples

100KG

20000

500

N

+

2

Cherry

200Kg

7000

1000

Y

+

3

Melon

300Kg

1000

900

N

+

4

Orange

150Kg

10000

1000

Y

+

5

Graphs

500Kg

2000

500

Y

+

6

Lemon

300Kg

5000

1500

Y

+



my need is the grid should work like this

S.No

Items

Quantity

Credit

Debit

Delivered

 

1

Apples

100KG

20000

500

Y

+

1a

Apples type1

50KG

1000

600

N

-

2

Cherry

200Kg

7000

1000

Y

+

3

Melon

300Kg

1000

900

N

+

3a

Melon type1

100Kg

500

100

N

-

3b

Melon type2

500Kg

5000

0

N

-

4

Orange

150Kg

10000

1000

Y

+

5

Graphs

500Kg

2000

500

Y

+

5

Graphs

500Kg

2000

500

Y

+

6

Lemon

300Kg

5000

1500

Y

+


i want to create the new rows wher i want
as like highlighted rows ..
How can i do this
I need Help For this

thanks in advance
MUTHU


Jitendra Faye replied to muthuraman alexander on 12-Sep-11 05:16 AM
Use this code-

protected void btnAdd_Click1(object sender, EventArgs e)

{

DataTable dTable = New DataTable();
DataColumn c1= new DataColumn();
DataColumn c2= new DataColumn();

dTable.Columns.Add(c1);
dTable.Columns.Add(c2);

dTable = GridView1.DataSource;


DataRow dr = dt.NewRow();

dr[0]= TxtBox1.Text;

dr[1]= TxtBox2.Text;

dTable.Rows.Add(dr);

GridView1.DataSource = dTable ;
GridView1.DataBind();

}

Try this code and let me know.

Reena Jain replied to muthuraman alexander on 12-Sep-11 05:59 AM
hi,

On button click, insert the new values in database, and then bind the gridview from database table again. So your gridview will display all the value from your database datable including your new value.

Hope this will help you
Radhika roy replied to muthuraman alexander on 12-Sep-11 10:22 AM

Follow these steps-

protected void btnAdd_Click(object sender, EventArgs e)

  {

    GridRowCreation(GridView1);

    TextBox txtName = (TextBox)GridView1.Rows[0].FindControl("txtName");

    txtName.Visible = true;

    Label lblName = (Label)GridView1.Rows[0].FindControl("lblName");

    lblName.Visible = false;

  }

  private void GridRowCreation(GridView GridView1)

  {

    DataSet ds = cc.GetData();

    DataTable dt = ds.Tables[0];

    GridView1.DataSource = InsertRow(dt);

    GridView1.DataBind();

  }

  private object InsertRow(DataTable dt)

  {

    DataRow dr = dt.DefaultView.Table.NewRow();

    dt.Rows.InsertAt(dr, 0);

    return dt;

  }

Hope this will help you.

 

dipa ahuja replied to muthuraman alexander on 12-Sep-11 11:02 AM
Gridview doesn't have inbuild method to insert values like update/delete. you have to implement it manually:


Untitled document
Step 1 : Add new template Field With the Required Controls:
 
<asp:TemplateField>
<FooterTemplate>
 Name:<asp:TextBox ID="txtname" runat="server" /><br />
 Gender :
 <asp:DropDownList ID="ddGender" runat="server">
   <asp:ListItem>Male</asp:ListItem>
   <asp:ListItem>Female</asp:ListItem>
 </asp:DropDownList>
       
  <asp:Button ID="btnInsert" runat="server" Text="Insert" OnClick="btnInsert_Click" />
</FooterTemplate>
</asp:TemplateField>
  
► Step 2 Implement the Button's Click event:
protected void btnInsert_Click(object sender, EventArgs e)
{
 string name = ((TextBox)GridView1.FooterRow.FindControl("txtname")).Text;
 string gender = ((DropDownList)GridView1.FooterRow.FindControl("ddGender")).SelectedValue;
  string ConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
 SqlConnection conn = new SqlConnection(ConnString);
   
 conn.Open();
   
 string q = "insert into people (name,gender) values(@name,@gender)";
 SqlCommand comm = new SqlCommand(q,conn);
 comm.Parameters.AddWithValue("name", name);
 comm.Parameters.AddWithValue("gender", gender);
 comm.ExecuteNonQuery();
 conn.Close();
 BindGrid(); //Re-bind Gridview
}