Friday 28 January 2022

DevExpress DataGrid - Add columns dynamically

This example demonstrates the DataGrid UI component communicating with a Web API service and allows to add columns dynamically.

  1. Create a CustomStore
  2. Make a call to API controller
  3. Process the controller response 
  4. Add columns dynamically in "onLoaded"


var dataGrid;
var responseData;
var col = new Array();

let customDataSource = new DevExpress.data.CustomStore({
key: "ID",
load: function (loadOptions) {
var deferred = $.Deferred();
$.ajax({
url: "/DataWorkbench/GetClientModel",
method: "GET"
})
.done(function (response) {
responseData = response;
deferred.resolve(JSON.parse(response.data), {
totalCount: response.totalCount
})
})
.fail(function (e) {
deferred.reject("Insertion failed");
});
return deferred.promise();
},
onLoaded: function (result) {

for (let i = 0; i < responseData.columns.length; i++) {
dataGrid.addColumn(responseData.columns[i]);
}
}
});



$(function () {

$("#gridContainer").dxDataGrid({
dataSource: customDataSource,
remoteOperations: true,
showBorders: true,
editing: {
mode: 'cell',
allowUpdating: true,
allowAdding: true,
allowDeleting: true,
},
customizeColumns: function (columns) {
columns[0].visible = false;

},
columns:[]
})

dataGrid = $("#gridContainer").dxDataGrid("instance");
});


Monday 23 June 2014

validation of viewstate MAC failed in ASP.NET Web Form

validation of viewstate MAC failed

There are few reason for this error. Below is one of them.

Check the idle time of application pool in IIS and try to increase.

  1. Open IIS
  2. Click on application pools node
  3. Locate your web application's application pool
  4. Right-Click and select Advanace Settings
  5. Set the Idle Time-out(minutes) property to 0 or increase 

Ref:
stackoverflow

Wednesday 19 February 2014

XML to Object serialization in ASP.NET Web API

Simple XML to Object serialization


Model

class Student
{
[XmlElement("Name")]
public string Name;

Controller

 [HttpPost]
  public string ProcessXML(Student s)
   {
        //code
   } 

Global asax.cs

var xml = GlobalConfiguration.Configuration.Formatters.XmlFormatter;
                 xml.SetSerializer<Student>(new XmlSerializer(typeof(Student))); 

Now you can post XML data as below
<Student>
<Name>balaji</Name>
</Student>
 

Tuesday 4 February 2014

Get gridview row data in c#

Grid

  <asp:GridView ID="grid" runat="server" AutoGenerateColumns="false"            onrowcommand="grid_RowCommand" >
 <Columns>
 <asp:TemplateField >
 <ItemTemplate>
 <asp:TextBox ID="txt"  runat="server" Text='<%#Eval("xxx") %>'  >                   </asp:TextBox>
  </ItemTemplate>
  </asp:TemplateField>
 <asp:TemplateField >
 <ItemTemplate>
<asp:LinkButton ID="lnk" CommandArgument=<%# Container.DataItemIndex + 1 %> CommandName="arg">Click</asp:LinkButton>
  </ItemTemplate>
  </asp:TemplateField>
  </Columns>
  </asp:GridView>

Code

 protected void grid_RowCommand(object sender, GridViewCommandEventArgs e)
{
int rowindex = int.Parse(e.CommandArgument.ToString());
((TextBox)(grid.Rows[rowindex].FindControl("txtgvunit"))).Text
}

Wednesday 22 January 2014

Deserialization of an XML to an object in C#

Deserialization of an XML to C# class

 XML
<Book Title="My Book">
   <Publisher Reference="XYZ123">Some Publisher</Publisher>
</Book>

Class

[XmlRoot("Book")]
public class Book
{
   [XmlAttribute]
   public string Title;

   [XmlElement]
   public Publisher Publisher;
}

[Serializable]
public class Publisher
{
  [XmlText]
  public string Value;

  [XmlAttribute]
  public string Reference;
}
 
 
Book res = new Book(); 
XmlTextReader reader = new XmlTextReader("file path"); 
res = (Book)serializer.Deserialize(reader);


Ref
stackoverflow

Tuesday 11 June 2013

DataTable Column to List in c#

Using LINQ:


table.AsEnumerable().Select(s => s["ID"].ToString()).ToList();


Wednesday 3 April 2013

Find controls in WPF

Consider this data grid in wpf

<DataGrid Name="dataGrid1">
  <DataGrid.Columns>                      
   <DataGridTemplateColumn >
    <DataGridTemplateColumn.CellTemplate>
     <DataTemplate>

               <StackPanel Name= "sp" >
                <TextBlock  Name="textBlock1" />                                                           </StackPanel>

      <DataTemplate>
   <DataGridTemplateColumn.CellTemplate>
 <DataGridTemplateColumn >
<DataGrid.Columns>       
</DataGrid>

To access element  "textBlock1" which is inside stackpanel, we can use the following code.


foreach (StackPanel sp in FindVisualChildren<StackPanel>(dataGrid1))
                    {
                        if (sp.Name == "sp")
                        {
                            TextBlock txt= (TextBlock)sp.FindName("textBlock1");       
                             txt.Text="Hello";
                          }
                        }
                   
 FindVisualChildren
  public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
        {
            if (depObj != null)
            {
                for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
                {
                    DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
                    if (child != null && child is T)
                    {
                        yield return (T)child;
                    }

                    foreach (T childOfChild in FindVisualChildren<T>(child))
                    {
                        yield return childOfChild;
                    }
                }
            }
        }