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
When the value of column sID_contents is greater than zero I can add a new row to the GridView.
I using this code Add new Row to DataTable and display in GridView using C# and VB.Net in ASP.Net
The problem using this code is that it adds two new rows to the GridView without being able to select which section the line should be added to
For example, in GridView I have
'Near 1', 1, 'D1C', 4
'Management 1', 1, 'D1C', 5
'Miss 1', 1, 'D1C', 6
I need insert new row on sID_contents equal to 1
'Near 1', 1, 'D1C', 4
'Management 1', 1, 'D1C', 5
'Miss 1', 1, 'D1C', 6
'Thread 1', 1, 'D1C', 10
The new row is
'Thread 1', 1, 'D1C', 10
and must be displayed along with all lines where `sID_contents` equal to 1
With this code instead the new row id added on all `sID_contents`
'Thread 1', 1, 'D1C', 10
'Thread 2', 2, 'D1C', 10
<asp:GridView ID="gvitems" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="sID" HeaderText="sID" ItemStyle-Width="30" />
<asp:BoundField DataField="contents" HeaderText="contents" ItemStyle-Width="120" />
<asp:BoundField DataField="sID_contents" HeaderText="sID_contents" ItemStyle-Width="100" />
<asp:BoundField DataField="sUnita" HeaderText="sUnita" ItemStyle-Width="70" />
</Columns>
</asp:GridView>
<br />
<asp:Button Text="Add Row" runat="server" OnClick="OnAddRow" />
using System.Data;
using System.Linq;
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = GetData();
gvitems.DataSource = dt;
gvitems.DataBind();
}
}
private static DataTable GetData()
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[]
{
new DataColumn("contents"),
new DataColumn("sID_contents"),
new DataColumn("sUnita"),
new DataColumn("sID")
{
DataType = typeof(int),
AutoIncrement = true,
AutoIncrementSeed = 1,
AutoIncrementStep = 1,
}
});
dt.Rows.Add("Near", 0, "D1C");
dt.Rows.Add("Management", 0, "D1C");
dt.Rows.Add("Miss", 0, "D1C");
dt.Rows.Add("Near 1", 1, "D1C");
dt.Rows.Add("Management 1", 1, "D1C");
dt.Rows.Add("Miss 1", 1, "D1C");
dt.Rows.Add("Near 2", 2, "D1C");
dt.Rows.Add("Management 2", 2, "D1C");
dt.Rows.Add("Miss 2", 2, "D1C");
return dt;
}
protected void OnAddRow(object sender, EventArgs e)
{
DataTable dt = GetData();
IEnumerable<DataTable> result = from table in dt.AsEnumerable()
group table by table["sID_contents"] into g
select g.CopyToDataTable();
for (int i = 0; i < result.ToList().Count; i++)
{
if (result.ToList()[i].Rows[0]["sID_contents"].ToString() != "0")
{
if (result.ToList()[i].Rows.Count <= 3)
{
DataRow dr = dt.NewRow();
dr["contents"] = "Thread " + i;
dr["sID_contents"] = result.ToList()[i].Rows[0]["sID_contents"];
dr["sUnita"] = result.ToList()[i].Rows[0]["sUnita"];
dt.Rows.Add(dr);
}
}
}
gvitems.DataSource = dt.AsEnumerable().OrderBy(x => x["sID_contents"]).CopyToDataTable();
gvitems.DataBind();
}