如何解决使用 C# 和 ASP.Net 向 DataTable 添加新的不同行并在 GridView 中显示
我正在 asp.net
与 c#
合作一个项目。
我从 mysql database
获取数据并使用此数据集填充 GridView
我想在 GridView 中根据列的顺序动态生成一个新行 sID_contents
例如在 GV 中我有:
'- Set n.2',2,'D1C',4
'Management 2',5
'Miss 2',6
我需要在 sID_contents
上插入等于 2 的新行
'- Set n.2',6
'New row Thread 2',10
新行是
'New row Thread 2',10
并且必须与所有 sID_contents
等于 2 的行一起显示
这是我的数据
-- ----------------------------
-- Table structure for t_contents
-- ----------------------------
DROP TABLE IF EXISTS `t_contents`;
CREATE TABLE `t_contents` (
`contents` varchar(1000) DEFAULT NULL,`sID_contents` int(11) DEFAULT NULL,`sUnita` varchar(255) DEFAULT NULL,`sID` int(11) NOT NULL AUTO_INCREMENT,PRIMARY KEY (`sID`) USING BTREE,UNIQUE INDEX `contents`(`contents`) USING BTREE
) ENGINE = InnoDB;
-- ----------------------------
-- Records of t_contents
-- ----------------------------
INSERT INTO `t_contents` VALUES ('- Set n.1',1,1);
INSERT INTO `t_contents` VALUES ('Management',2);
INSERT INTO `t_contents` VALUES ('Miss',3);
INSERT INTO `t_contents` VALUES ('- Set n.2',4);
INSERT INTO `t_contents` VALUES ('Management 2',5);
INSERT INTO `t_contents` VALUES ('Miss 2',6);
INSERT INTO `t_contents` VALUES ('- Set n.3',3,7);
INSERT INTO `t_contents` VALUES ('Management 3',8);
INSERT INTO `t_contents` VALUES ('Miss 3',9);
这是我背后的代码
public partial class gv : System.Web.UI.Page
{
protected void Page_Load(object sender,EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[]
{
new DataColumn("contents"),new DataColumn("sID_contents"),new DataColumn("sUnita"),new DataColumn("sID")
});
dt.Rows.Add("- Set n.1","D1C",1);
dt.Rows.Add("Management",2);
dt.Rows.Add("Miss",3);
dt.Rows.Add("- Set n.2",4);
dt.Rows.Add("Management 2",5);
dt.Rows.Add("Miss 2",6);
dt.Rows.Add("- Set n.3",7);
dt.Rows.Add("Management 3",8);
dt.Rows.Add("Miss 3",9);
gvitems.DataSource = dt;
gvitems.DataBind();
}
}
protected void gvProducts_RowDataBound(object sender,GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string lbset = (string)DataBinder.Eval(e.Row.DataItem,HttpUtility.HtmlDecode("contents"));
if (lbset.ToString().StartsWith("- Set "))
{
ImageButton image = new ImageButton();
image.ImageUrl = "img/add-page-red.gif";
image.ToolTip = "Add new row to " + HttpUtility.HtmlDecode(lbset)
.ToString()
.Replace("<h4><strong>","")
.Replace("</strong></h4>","");
e.Row.Cells[2].Controls.Add(image);
}
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。