2014年7月13日 星期日

[RESOLVED] How to add dynamic row in the gridview?


Hello,


I want to add new row dynamically in the grid at the first position(before all rows or top row or row '0' always). And the remaining rows having textbox in disable mode. Only the top row is for adding records. old rows are in disable mode(view mode).


Code:


            PageSize="30" runat="server" GridLines="None">

<%-- Date --%>
DataField="Date"
HeaderStyle-Width="70"
HeaderText="Date">






<%-- Name--%>
DataField="Name" HeaderStyle-Width="50"
HeaderText="Name">






<%--Comments--%>
DataField="Comments"
HeaderStyle-Width="120"
HeaderText="Comments">












protected void Page_Load(object sender, EventArgs e)
{
try
{

if (!IsPostBack)
{
DataSet ds = new DataSet();
ds = objGetData.Getds(RecNo); // loading Dataset from table


//int rowIndex =0;

DataRow dr1 = dsInDiscussion.Tables[0].NewRow();
dr1["Date"] = DateTime.Now;
dr1["User_Name"] = “John”;
dr1["Comments"] = "";
dt.Rows.Add(dr1);

ds.Tables.Add(dt);

FillGrid();
}
}
else
{
Response.Redirect("Logout.aspx", true);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}

protected void FillGrid() {
if (ds.Tables[0].Rows.Count > 0)
{
GridView1.DataSource = ds;
GridView1.Visible = true;
}
else
{
GridView1.DataSource = null;
GridView1.Visible = false;
}

}

Dynamically created row contains "Date(Label), Name(Label) and Comments(TextBox - in edit mode) which is first row and all rows data are in view mode non editable.


Please suggest me, where can i have to modify the code?


Thanks,


Venkata.




Below link has the example to create the new row inside a gridview. You can disable all the controls before adding this new row.


http://www.dotnetobject.com/Thread-Adding-Empty-Rows-in-GridView





Hello,


I have added dynamic row as first row :


//Adding Row Dynamically at first position rowIndex =0

DataRow dr1 = dsInDiscussion.Tables[0].NewRow();
dr1["Date"] = DateTime.Now;
dr1["User_Name"] = “John”;
dr1["Comments"] = "";

ds.Tables.InsertAt(dr1, 0); // Specifying RowIndex = 0 here

Thanks,


Venkata.


沒有留言:

張貼留言