My requirement is on pageload since I've nothing to bind to radgrid I need to just show the grid headers with "Add new row" button".
1. When the user click the "Add new row' button a new row is created with 2 textboxes and 2 dropdown(binds data from the database) to the radgrid row.
2. Each row should have remove row button
3. at the radgrid footer I've the Button "save to db"
4. after adding the data to the new row(not saved yet to the db) i need to add more rows. When I click on "Add new row' button I lose all the data entered. Technically saying on postback losing all data. this part is where I need help on how to handle this.
Finally after adding all rows I save the data to database.
*************Thoughout this functionality I have no data to rebind the radgrid from the database**************************
<
telerik:RadGrid
ID
=
"RadGrid1"
runat
=
"server"
AutoGenerateColumns
=
"false"
MasterTableView-CommandItemSettings-ShowAddNewRecordButton
=
"false"
MasterTableView-CommandItemSettings-ShowRefreshButton
=
"false"
OnNeedDataSource
=
"RadGrid1_NeedDataSource"
OnItemDataBound
=
"RadGrid1_ItemDataBound"
OnItemCommand
=
"RadGrid1_ItemCommand"
>
<
MasterTableView
Width
=
"100%"
HeaderStyle-Font-Bold
=
"true"
CommandItemStyle-Font-Bold
=
"true"
DataKeyNames
=
"IsAdd,CourseID"
CommandItemDisplay
=
"Top"
CommandItemStyle-HorizontalAlign
=
"Right"
>
<
CommandItemTemplate
>
<
asp:Button
ID
=
"IsAdd"
Font-Size
=
"Small"
Font-Bold
=
"true"
CommandName
=
"InitInsert"
Text
=
"Add Course"
runat
=
"server"
/>
</
CommandItemTemplate
>
<
Columns
>
<
telerik:GridTemplateColumn
UniqueName
=
"CourseID"
HeaderText
=
"Course #"
DataField
=
"CourseID"
>
<
ItemTemplate
>
<
asp:TextBox
ID
=
"TextBox1"
runat
=
"server"
></
asp:TextBox
>
</
ItemTemplate
>
</
telerik:GridTemplateColumn
>
<
telerik:GridTemplateColumn
UniqueName
=
"CourseDept"
HeaderText
=
"Course dept"
DataField
=
"DeptID"
>
<
ItemTemplate
>
<
asp:DropDownList
ID
=
"DeptDropDown"
DataField
=
"DeptID"
DataTextField
=
"DeptName"
runat
=
"server"
AutoPostBack
=
"false"
></
asp:DropDownList
>
</
ItemTemplate
>
</
telerik:GridTemplateColumn
>
<
telerik:GridTemplateColumn
UniqueName
=
"LocationName"
HeaderText
=
"Course Location"
DataField
=
"LocationID"
>
<
ItemTemplate
>
<
asp:DropDownList
ID
=
"locationDropDown"
DataTextField
=
"locationName"
runat
=
"server"
AutoPostBack
=
"false"
></
asp:DropDownList
>
</
ItemTemplate
>
</
telerik:GridTemplateColumn
>
<
telerik:GridTemplateColumn
UniqueName
=
"Name"
HeaderText
=
"Remarks"
>
<
ItemTemplate
>
<
asp:TextBox
ID
=
"TextBox4"
runat
=
"server"
></
asp:TextBox
>
<
asp:Button
ID
=
"Button1"
runat
=
"server"
Text
=
"Remove Row"
CommandName
=
"Delete"
/>
</
ItemTemplate
>
</
telerik:GridTemplateColumn
>
</
Columns
>
</
MasterTableView
>
</
telerik:RadGrid
>
<
asp:Button
ID
=
"savebtn"
runat
=
"server"
Font-Bold
=
"true"
Text
=
"Save the course"
/>
aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
if (!IsPostBack)
{
dt.Columns.Add("CourseID");
dt.Columns.Add("Name");
dt.Columns.Add("IsAdd");
Session["dt"] = dt;
}
}
protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
RadGrid1.DataSource = (DataTable)Session["dt"];
}
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
Button btn = new Button();
if (e.Item is GridDataItem)
{
GridDataItem item = e.Item as GridDataItem;
TextBox TextBox1 = item.FindControl("TextBox1") as TextBox;
Button Button1 = item.FindControl("Button1") as Button;
(I've deptdropdown and locationdropdown binded here)
TextBox TextBox4 = item.FindControl("TextBox4") as TextBox;
bool isAdd = Convert.ToBoolean(item.GetDataKeyValue("IsAdd"));
if (isAdd)
{
TextBox1.Visible = LocationDropDown.Visible = DeptDropDown.Visible = TextBox4.Visible = true;
btn.Visible = true;
RadGrid1.DataSource = Session["dt"];
}
}
}
protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
if (e.CommandName == RadGrid.InitInsertCommandName)
{
DataTable dt = (DataTable)Session["dt"];
dt.Rows.Add(0, string.Empty, true);
RadGrid1.MasterTableView.IsItemInserted = false;
e.Canceled = true;
RadGrid1.Rebind();
}
}
}
KIndly help me solve this. Provided the code below.