简体   繁体   中英

Adding row to gridview using button in c# winform and sql server

I am working on project about student grading system. I am stucked the point that ı will be showing below:

STUDENT ID : 12345
STUDENT NAME : ALEXIA

LESSONS :          GRADE 1     GRADE 2  AVERAGE   LIMIT    RESULT
PHYSICS             40          80         60      50        PASS
CALCULUS            20          10         15      50        FAIL
Add new lesson

New list should be ; (assume we add Sport as new lesson on the list)

    LESSONS :          GRADE 1     GRADE 2  AVERAGE   LIMIT    RESULT
     PHYSICS             40          80         60      50        PASS
     CALCULUS            20          10         15      50        FAIL
     SPORT               100         100       100      50        PASS
    add new lesson (line)

I will save such data to database then list all data on new form as;

  STUDENT NAME : ALEXIA

  LESSONS          GRADE          RESULT
   PHYSICS           60             PASS
   CALCULUS          15             FAIL
   SPORT            100             PASS

How I can develop such structure here . May I need to use grid or list

Advices will really be appreciated

If you are looking for something so simple as add new row to gridview by pression a button and some textbox or something try this

DataGridViewRow row = (DataGridViewRow)yourDataGridView.Rows[0].Clone();
row.Cells["LESSONS"].Value = yourtextbox.text;
row.Cells["Grade"].Value = yourtextbox1.text;
row.Cells["Result"].Value = yourtextbox1.text;
yourDataGridView.Rows.Add(row);

1st would like to apologize for miss reading , now that i see is is in winforms just right click on your project in the solution explorer and choose 'Add new User Control'. This will actually let you pick from a number of different kinds of objects to add, but user controls is the one you want. When you add the User Control it should give you a new designer window with a panel all by itself onto which you can drop regular controls. If you then go back to your form you'll see a new section in the toolbox that includes your new control. You can use it just like any other control, including dynamically creating and adding it to a flowlayoutpanel on your form.

Then add a row on datagrid for example as simple as this

public DataTable GetResultsTable()
{
    DataTable table = new DataTable();
    table.Columns.Add("Name".ToString());
    table.Columns.Add("Color".ToString());
    DataRow dr = table.NewRow();
    dr["Lesson"] = "Programing";
    dr["Calculus"] = "99";
   dr["result"] = "pass";
    table.Rows.Add(dr);
    return table;
}
public void gridview()
{          
    datagridview1.DataSource =  GetResultsTable();
}

Hope this time i helped you go in the right way mate, good luck

I dont know if my this is what you pretend but, lets see..

For what i could understand you want something like this

STUDENT NAME : ALEXIA

  LESSONS          GRADE          RESULT
   PHYSICS           60             PASS
   CALCULUS          15             FAIL
   SPORT            100             PASS

So you basicly can create one html table structure and use

<%# (DataBinder.Eval(Container, "DataItem.username")) %> To bind your items from DB

Now answering your possible questions:

But i want 3 , 4 or 100 structures like this depending the number of stundes i get from db.

Easy mate, put ur structure in a repeater and just assing the datasource to the repeater and you will get the structure according to the data.


But i dont have only physics, calculus or sport i can add more .

Just use another repater inside repeater with something like this

<td> <%#(DataBinder.Eval(Container, "DataItem.Title")) %></td><td> <%#(DataBinder.Eval(Container, "DataItem.Grade")) %></td><td> <%#(DataBinder.Eval(Container, "DataItem.Result")) %></td>

* PS: remember the dataiitems i use are just example...

Problem accessing repeater inside repeater? Use something like this

 protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    var data = ((MyClass)e.Item.DataItem).Subcategories;
    var repeater2 = (Repeater)e.Item.FindControl("Repeater2");
    repeater2.DataSource = data;
    repeater2.DataBind();
}

Dont know if it was this what you where looking for, but hope it helps in any way. Good luck

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM