本帖最后由 飘移 于 2012-10-10 05:37 编辑
作者官方的Demo中没有提供ext:Grid绑定JSON数组的例子,这里我来提供一个ext:Grid绑定JSON数组的例子,抛砖引玉,供初学者参考,高手可以略过:
以下代码放在cs代码文件中:
- protected static JArray JsonStudents = JArray.Parse(
- "[{"Id":101,"Name":"陈萍萍","EntranceYear":"2000-9-1","AtSchool":true,"Major":"计算机","Group":1,"Gender":0,"LogTime":"2011-03-08","Desc":"陈萍萍,女,20岁,出生于中国南方的一个小山村,毕业于中国科学技术大学。","Guid":"" + Guid.NewGuid().ToString() + "","Hobby":"打麻将"}," +
- "{"Id":102,"Name":"陈萍萍","EntranceYear":"2000-9-1","AtSchool":true,"Major":"计算机","Group":1,"Gender":0,"LogTime":"2011-03-08","Desc":"陈萍萍,女,20岁,出生于中国南方的一个小山村,毕业于中国科学技术大学。","Guid":"" + Guid.NewGuid().ToString() + "","Hobby":"打麻将"}," +
- "{"Id":103,"Name":"陈萍萍","EntranceYear":"2000-9-1","AtSchool":true,"Major":"计算机","Group":1,"Gender":0,"LogTime":"2011-03-08","Desc":"陈萍萍,女,20岁,出生于中国南方的一个小山村,毕业于中国科学技术大学。","Guid":"" + Guid.NewGuid().ToString() + "","Hobby":"打麻将"}," +
- "{"Id":104,"Name":"陈萍萍","EntranceYear":"2000-9-1","AtSchool":true,"Major":"计算机","Group":1,"Gender":0,"LogTime":"2011-03-08","Desc":"陈萍萍,女,20岁,出生于中国南方的一个小山村,毕业于中国科学技术大学。","Guid":"" + Guid.NewGuid().ToString() + "","Hobby":"打麻将"}]"
- );
- private void BindGrid()
- {
- Grid2.DataSource = JsonStudents;
- Grid2.DataBind();
- }
- protected string GetGender(object gender)
- {
- if (Convert.ToInt32(gender.ToString()) == 1)
- {
- return "男";
- }
- else
- {
- return "女";
- }
- }
复制代码
特殊处理:在本例中,我对官方Demo的GetGender方法进行了完善,因为gender传入的是JValue对象,无法直接转换成Int32,所以必须将gender用ToString()转换为string再转换为Int32,否则会抛出异常。
|