简体   繁体   English

使用C#Winform和SQL Server中的按钮将行添加到gridview

[英]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 如果您正在寻找简单的操作,例如通过按下按钮和一些文本框或将某些新内容添加到gridview,请尝试以下操作

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'. 1st想为未读内容表示歉意,现在我看到的是Winforms,只需在解决方案资源管理器中右键单击您的项目,然后选择“添加新用户控件”即可。 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. 您可以像使用任何其他控件一样使用它,包括动态创建它并将其添加到表单上的flowlayoutpanel。

Then add a row on datagrid for example as simple as this 然后在datagrid上添加例如这样的一行

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 因此,您基本上可以创建一个html表结构并使用

<%# (DataBinder.Eval(Container, "DataItem.username")) %> To bind your items from DB <%#(DataBinder.Eval(Container,“ DataItem.username”))%>绑定数据库中的项目

Now answering your possible questions: 现在回答您可能的问题:

But i want 3 , 4 or 100 structures like this depending the number of stundes i get from db. 但是我想要3、4或100个这样的结构,具体取决于我从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... * PS:请记住,我使用的数据只是示例...

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 祝好运

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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