Hi,
I am working on a project in asp.net with c#.
I take data from mysql database and with this data set populate GridView.
I want to dynamically generate a new Row in GridView respecting the sequence of the column sID_contents.
For example in GV I have:
'- Set n.2', 2, 'D1C', 4
'Management 2', 2, 'D1C', 5
'Miss 2', 2, 'D1C', 6
I need insert new row on sID_contents equal to 2
'- Set n.2', 2, 'D1C', 4
'Management 2', 2, 'D1C', 5
'Miss 2', 2, 'D1C', 6
'New row Thread 2', 2, 'D1C', 10
The new row is
'New row Thread 2', 2, 'D1C', 10
and must be displayed along with all lines where sID_contents equal to 2
This is my data
-- ----------------------------
-- 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, 'D1C', 1);
INSERT INTO `t_contents` VALUES ('Management', 1, 'D1C', 2);
INSERT INTO `t_contents` VALUES ('Miss', 1, 'D1C', 3);
INSERT INTO `t_contents` VALUES ('- Set n.2', 2, 'D1C', 4);
INSERT INTO `t_contents` VALUES ('Management 2', 2, 'D1C', 5);
INSERT INTO `t_contents` VALUES ('Miss 2', 2, 'D1C', 6);
INSERT INTO `t_contents` VALUES ('- Set n.3', 3, 'D1C', 7);
INSERT INTO `t_contents` VALUES ('Management 3', 3, 'D1C', 8);
INSERT INTO `t_contents` VALUES ('Miss 3', 3, 'D1C', 9);
This is my code behind
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", 1, "D1C", 1);
dt.Rows.Add("Management", 1, "D1C", 2);
dt.Rows.Add("Miss", 1, "D1C", 3);
dt.Rows.Add("- Set n.2", 2, "D1C", 4);
dt.Rows.Add("Management 2", 2, "D1C", 5);
dt.Rows.Add("Miss 2", 2, "D1C", 6);
dt.Rows.Add("- Set n.3", 3, "D1C", 7);
dt.Rows.Add("Management 3", 3, "D1C", 8);
dt.Rows.Add("Miss 3", 3, "D1C", 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);
}
}
}
}