Hi
I am trying to add new row in WebDataGrid by clicking button. I am using Infragistics 2012.1 version. But it is not creating. Is there any sample or suggessions?
I appreciate if you have sample code for server-side as well.
.aspx
function AddRow() { var grid = $find("WebDataGrid1"); row = new Array(grid.get_columns().get_length()); // create a new empty row grid.get_rows().add(row); } </script></head><body> <form id="form1" runat="server"> <ig:WebScriptManager ID="WebScriptManager1" runat="server"> </ig:WebScriptManager> <ig:WebDialogWindow ID="WebDialogWindow1" runat="server" Width="50%" Height="50%" WindowState="Hidden" /> <div> <ig:WebDataGrid ID="WebDataGrid1" runat="server" Visible="true" Height="350px" Width="400px" AutoGenerateColumns="false"> <Columns> <ig:BoundDataField DataFieldName="Name" Key="Name"> <Header Text="Name" /> </ig:BoundDataField> <ig:BoundDataField DataFieldName="Type" Key="Type"> <Header Text="Type" /> </ig:BoundDataField> <ig:UnboundField Key="Id" Hidden="true"> <Header Text="Id" /> </ig:UnboundField> </Columns> <Behaviors> <ig:EditingCore AutoCRUD="false"> <Behaviors> <ig:RowAdding Enabled="false"/> </Behaviors> </ig:EditingCore> </Behaviors> </ig:WebDataGrid> </div> <input type="button" value="Add" onclick="AddRow()" />
</form></body>
Code-Behind
protected void Page_Load(object sender, EventArgs e) { List<Employee> Details = new List<Employee>(); for (int i = 0; i < 2; i++) { Employee obj = new Employee(); obj.Name = "David"; obj.Type = "Employee"; obj.Id = i; Details.Add(obj); } WebDataGrid1.DataSource = Details; WebDataGrid1.DataBind();
}}public class Employee{ public int Id { get; set; } public string Name { get; set; } public string Type { get; set; } }
Hi Jon,
I also going through the same issue, would like to add row at top instead of bottom. please let me know if there is a way to implement this.
Regards,
Ahmed
I've followed the example you've given, but I'm not being able getting it work. I only get empty rows added to my grid...can't realize why this is happening....here's the code:
I'd REALLY appreciate if you could give me a hand with this, I've been fighting since a week with this :S
Diego.
ASPX:
<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="AssetNew.Templates.WebForm2" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server"></asp:Content><asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> <script type="text/javascript">
function AddRow() { debugger; var grid = $find("<%= wdgComponents.ClientID %>"); var id = '22'; var name = document.getElementById("<%= txtComponentName.ClientID %>").value; var description = document.getElementById("<%= txtComponentDescription.ClientID %>").value; var equipmentTypeId = document.getElementById("<%= cbxComponentEqType.ClientID %>").value; var eqType = document.getElementById("<%= cbxComponentEqType.ClientID %>");
var row = new Array(id, name, description, equipmentTypeId, eqType.options[eqType.selectedIndex].text); // create a new empty row grid.get_rows().add(row); } </script> <asp:ScriptManager ID="ScriptManager1" runat="server"> </asp:ScriptManager> <br /> <br /> <table border="1"> <tr> <td class="style2"> <asp:Label ID="Label444" runat="server" Text="Name"></asp:Label> </td> <td> <ig:WebTextEditor ID="txtComponentName" runat="server" EnableAjaxViewState="False" EnableViewState="False" ValidationGroup="validationComponents" Width="300px"> </ig:WebTextEditor> <asp:RequiredFieldValidator ID="rfvName" runat="server" ControlToValidate="txtComponentName" ErrorMessage="*" ValidationGroup="validationComponents"></asp:RequiredFieldValidator> </td> </tr> <tr> <td class="style2"> <asp:Label ID="Label3" runat="server" Text="Description"></asp:Label> </td> <td> <ig:WebTextEditor ID="txtComponentDescription" runat="server" ValidationGroup="validationComponents" Width="300px"> </ig:WebTextEditor> <asp:RequiredFieldValidator ID="rfvDescription" runat="server" ControlToValidate="txtComponentDescription" ErrorMessage="*" ValidationGroup="validationComponents"></asp:RequiredFieldValidator> </td> </tr> <tr> <td class="style2"> <asp:Label ID="Label1" runat="server" Text="Equipment Type"></asp:Label> </td> <td> <asp:DropDownList ID="cbxComponentEqType" runat="server" CausesValidation="True" Width="304px"> </asp:DropDownList> <asp:RequiredFieldValidator ID="rfvEqType" runat="server" ControlToValidate="cbxComponentEqType" ErrorMessage="*" InitialValue="0" ValidationGroup="validationComponents"></asp:RequiredFieldValidator> <br /> <br /> </td> </tr> <tr> <td style="visibility: hidden"> <ig:WebTextEditor ID="txtComponentId" runat="server"> </ig:WebTextEditor> </td> <td> <asp:Button ID="btnSubmit" runat="server" OnClick="btnSubmit_Click" OnClientClick="return AddRow()" Text="Submit" ValidationGroup="validationComponents" /> </td> </tr> </table> <br /> <br /> <ig:WebDataGrid ID="wdgComponents" runat="server" Height="350px" Width="400px" AutoGenerateColumns="False" onrowadding="WebDataGrid1_RowAdding"> <Columns> <ig:BoundDataField DataFieldName="ID" Key="ID"> <Header Text="ID" /> </ig:BoundDataField> <ig:BoundDataField DataFieldName="Name" Key="Name"> <Header Text="Name" /> </ig:BoundDataField> <ig:BoundDataField DataFieldName="Description" Key="Description"> <Header Text="Description" /> </ig:BoundDataField> <ig:BoundDataField DataFieldName="EquipmentTypeId" Key="EquipmentTypeId"> <Header Text="Equipment Type ID" /> </ig:BoundDataField> <ig:BoundDataField DataFieldName="EquipmentType" Key="EquipmentType"> <Header Text="Equipment Type" /> </ig:BoundDataField> </Columns> <Behaviors> <ig:EditingCore AutoCRUD="False"> <Behaviors> <ig:RowAdding> </ig:RowAdding> </Behaviors> </ig:EditingCore> </Behaviors> </ig:WebDataGrid></asp:Content>
ASPX.CS:
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using DTO;using BLL;using Infragistics.Web.UI.GridControls;using Infragistics.Web.UI.ListControls;
using System.Data;using System.Web.UI.WebControls;
namespace AssetNew.Templates{ public partial class WebForm2 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { DataTable dt2 = new DataTable(); DataRow dr = null; dt2.Columns.Add("ID"); dt2.Columns.Add("Name"); dt2.Columns.Add("Description"); dt2.Columns.Add("EquipmentTypeId"); dt2.Columns.Add("EquipmentType");
if (!IsPostBack) {
Session["MyList"] = dt2; }
wdgComponents.DataSource = Session["MyList"]; wdgComponents.DataBind();
TemplateBLL oTemplateBLL=new TemplateBLL();
List<String[]> lstEqTypes = oTemplateBLL.GetEquipmentTypes();
fillDropDownLists(cbxComponentEqType, lstEqTypes, "-- Select Equipment Type --");
// DataTable dty = new DataTable(); // wdgComponents.DataSource = dty;
}
protected void fillDropDownLists(WebDropDown _oDropDownList, List<String[]> _listValues, string firstItem) { _oDropDownList.Items.Add(new DropDownItem(firstItem, "0"));
foreach (var item in _listValues) { _oDropDownList.Items.Add(new DropDownItem(item[1], item[0])); } } protected void fillDropDownLists(DropDownList _oDropDownList, List<String[]> _listValues, string firstItem) { _oDropDownList.Items.Add(new System.Web.UI.WebControls.ListItem(firstItem, "0"));
foreach (var item in _listValues) { _oDropDownList.Items.Add(new System.Web.UI.WebControls.ListItem(item[1], item[0])); } }
protected void WebDataGrid1_RowAdding(object sender, Infragistics.Web.UI.GridControls.RowAddingEventArgs e) { { var MyList = (DataTable)Session["MyList"]; //MyList.Add(new Employee() { Name = (string)e.Values["Name"], Type = (string)e.Values["Type"] }); //loyee() { Name = (string)e.Values["Name"], Type = (string)e.Values["Type"] });
DataRow dr = MyList.NewRow(); dr["ID"] = MyList.Rows.Count.ToString(); dr["Name"] = (string)e.Values["Name"]; dr["Description"] = (string)e.Values["Description"]; dr["EquipmentTypeId"] = (string)e.Values["Description"]; dr["EquipmentType"] = (string)e.Values["EquipmentType"]; MyList.Rows.Add();
Session["MyList"] = MyList; } }
protected void btnSubmit_Click(object sender, EventArgs e) {
} }}
Hello Jon,
I have the similar requirement to add new row to the grid on client side and this works well by using
function AddRow() { var grid = $find("WebDataGrid1"); var row = new Array(grid.get_rows().length, "Name", "Employee"); // create a new empty row grid.get_rows().add(row); }
This adds the new row to the end of the grid ( as we are selecting the total rows and then adding it to those rows)
My requirement is to add the new row to the top of the grid. Is there any way to add it at the beginning of the grid rather than adding at end .
I am using the Version=11.2.20112.2176 version of infragistics of build version for .net framework 4.0
Thanks,
prashanth
Hello InfraOracle,
To answer your last question, the dropdown providers cannot be selected by row because each row uses the same dropdownprovider. However, depending on what you are trying to do with the control, you can get the dropdownprovider on a cell when you select the cell.
I hope that helps clarify things.
Sincerely,JonInfragistics, Inc.http://ko.infragistics.com/help
Thank you for quick reply Jon. Working!!