4

i'm making ASP.net application and i want to have some kind of table there. I tried using Grindview, but when i try to add second new row (from code), the second row replaces first row.

here is code:

DataTable dt = new DataTable();

            if (dt.Columns.Count == 0)
            {
                dt.Columns.Add("thing", typeof(string));
                dt.Columns.Add("thing2", typeof(string));
            }
            DataRow NewRow = dt.NewRow();
            NewRow[0] = label1.Text;
            NewRow[1] = label2.Text;
            dt.Rows.Add(NewRow);
            GridView1.DataSource = dt;
            GridView1.DataBind();

I'm expecting Gridview with 2 rows and button which gives you new row with every click.

Chris Catignani
  • 5,040
  • 16
  • 42
  • 49
TrotlOtl
  • 103
  • 1
  • 1
  • 9

3 Answers3

1

I dont know what you exactly asking or trying to do but this might help

  protected void Button1_Click(object sender, EventArgs e)

  {

   DataTable dt = new DataTable();

   if (dt.Columns.Count == 0)
   {
       dt.Columns.Add("nameofcolumn1", typeof(string));
       dt.Columns.Add("nameofcolumn2", typeof(string));
       dt.Columns.Add("nameofcolumn3", typeof(string));
   }

   DataRow NewRow = dt.NewRow();
   NewRow[0] = value1;
   NewRow[ 1] = value2;
   dt.Rows.Add(NewRow); 
   GridView1.DataSource = dt;
   GridViewl.DataBind();

   }

visit this link also Add row datagridview at every button click

Community
  • 1
  • 1
  • When I click a button, this code makes Gridview with one row, when i click again, the new row replaces the old row. The second row must stay below the first row. – TrotlOtl Jun 27 '16 at 17:55
  • Check the link below that will help you –  Jun 27 '16 at 18:10
1

You can add rows using a loop, where you create the row, add the content and then add the row to the datatable.

DataTable dt = new DataTable();

if (dt.Columns.Count == 0)
{
     dt.Columns.Add("thing", typeof(string));
     dt.Columns.Add("thing2", typeof(string));
}

for(int i = 0; i < 3; i++)
{
     DataRow dr = dt.NewRow();
     dr[0] = "foo";
     dr[1] = "bar";
     dt.Rows.Add(dr);
}

Then bind the datatable to your GridView:

GridView1.DataSource = dt;
GridViewl.DataBind();
devlin carnate
  • 8,309
  • 7
  • 48
  • 82
1

if you have your front hand HTML asp.net code that define your gridview's style (here's an exemple) :

 <asp:GridView ID="grvModel"  runat="server" AlternatingRowStyle-BackColor="#eeeeee" BackColor="#aaccff"  AutoGenerateColumns="false">
    <Columns>

        <asp:TemplateField >
                <ItemTemplate>
                    <asp:Button id="btnDel" CommandName="Delete" OnClientClick="btnDel_click" runat="server"Text="Delete" />
                </ItemTemplate>
         </asp:TemplateField>

        <asp:TemplateField >
                <ItemTemplate>
                    <asp:TextBox runat="server" ID="lblWrite" Text='write here' />
                </ItemTemplate>
         </asp:TemplateField>
    </Columns>
 </asp:GridView>

Then all you have to do is create a List<> of object and bind it to your gridView, if you want to add somthing, just add to the list and bind it again (hers's an exemple) :

List<aClass> classList;

protected void BindFirstTime(object sender, EventArgs e)
{
    classList= new List<aClass>();
    classList.Add(new aClass("one"));
    classList.Add(new aClass("two"));
    Grv.DataSource = classList;
    Grv.DataBind();
}

protected void AddObject(object sender, EventArgs e)
{
    classList.Add(new aClass("three or more !"));
    Grv.DataSource = classList;
    Grv.DataBind();
}

This is the simple way for me, hope it helps.

Antoine Pelletier
  • 3,164
  • 3
  • 40
  • 62
  • What exactly is aClass and db? – TrotlOtl Jun 27 '16 at 18:30
  • sorry, aClass is a class, any class you've made, and you weren't suposed to see db at all, my mistake, but if you'r curious, it was Entity-Linq code, and i use it to query a database. By using the db object you saw, i can show datas (from a database) in a gridview with a few lines of code... A nice thing, fast and easy – Antoine Pelletier Jun 27 '16 at 18:42
  • And what should i place instead of aClass? string? Sorry, but I don't understand this.. – TrotlOtl Jun 27 '16 at 18:48
  • You have to look at your gridview design, if it requires a propertys like (id, name, age) then your class must have a property called id, another one called name, and another one called age. exemple of a property : `string name {get; set;}` – Antoine Pelletier Jun 27 '16 at 18:52
  • Now that i think of it, could you show the design of your gridview in you'r question ? – Antoine Pelletier Jun 27 '16 at 18:54
  • Gridview design? It looks just like default gridview when you drag and drop it from toolbox. And what do you mean with "if it requires a propertys" ? It doesn't require nothing... – TrotlOtl Jun 27 '16 at 19:10
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/115759/discussion-between-trotlotl-and-antoine-pelletier). – TrotlOtl Jun 27 '16 at 19:14