重新认识GridView的使用方式
时间:2011-06-07 来源:馨倾雨笛
工作以来,对Gridview始终保持着相对比较厌烦的态度,总感觉Gridview的使用是比较死板的,没有灵活性。。。可是最近的工作中让我觉得之前的想法是挺可笑的,原因是我们的认识太过片面了。不管什么控件,只要使用在对的地方就能发挥最大的效率。尤其是跟ObjectDataSet配合使用,效率挺高的(我指的是工作效率);
晚上闲着没事,就稍微的摘记一下(嘿嘿),关于GridView我就不多说了,重点来说明下ObjectDataSet的使用在aspx中的部分代码如下:
<asp:ObjectDataSource ID="odsTeach" runat="server"
SelectMethod="QueryTeach" TypeName="QH.CRM.BLL.TeachManager" SelectCountMethod="ArticleCount" EnablePaging="True">
<SelectParameters>
<asp:Parameter Name="key" Type="String" />
<asp:Parameter Name="categoryId" Type="Int32" />
</SelectParameters>
</asp:ObjectDataSource>
这里说明下EnablePaging="True"的作用:
1.如果不需要分页时(显示所有的数据)则要设置为false;
2.需要分页时,则为true,这时要注意的是ods会为我们自动传入两个分页参数int startRowIndex, int maximumRows且注意大小写及单词结构都要一样,所调用的方法中也要带上这两个参数。
我这边的cs实现代码是这样的(从Bll开始到Dal的调用及各个相关类代码的调用):
private int _Total = 0;View Code(DAL)
public DataSet QueryTeach(int startRowIndex, int maximumRows, string key, int categoryId)
{
string strWhere = "1=1";
string orderBy = " TeachId DESC";
if (categoryId > 0)
{
strWhere = " And TeachCategoryId=" + categoryId;
}
if (!string.IsNullOrEmpty(key))
{
strWhere += string.Format(" And Teach.TeachName like '%{0}%' ", key);
}
var ps = dal.QueryTeach(startRowIndex, maximumRows, strWhere, orderBy);
_Total = ps.Total;
return ps.Data;
}
public int ArticleCount(string key, int categoryId)
{
return _Total;
}
public PagedDataSet QueryTeach(int startRowIndex, int maximumRows, string strWhere, string orderby)View Code(相关函数)
{
string cmdText = @"select TeachId,Teach.TeachName,TeachCategoryId,TeachAddUser,Employe.Name,TeachAddTime,TeachFiles from Teach inner join Employe On Teach.TeachAddUser=Employe.EmployeId where {0} Order by {1} ";
if (string.IsNullOrEmpty(strWhere)) strWhere = "1=1";
cmdText = string.Format(cmdText, strWhere, orderby);
Database db = DatabaseFactory.CreateDatabase();
using (IDataReader dr = db.ExecuteReader(CommandType.Text, cmdText))
{
int total = 0;
var ds = DBH.LoadDataSet(startRowIndex, maximumRows, dr, out total);
return new PagedDataSet { Data = ds, Total = total };
}
}
public static DataSet LoadDataSet(int startRowIndex, int maximumRows, IDataReader reader, out int total)上面都是结合ObjectDataSet的使用小结,希望对大家有所帮助。关于GridView的行和列的操作(待续......)
{
DataSet ds = new DataSet();
ds.Tables.Add(Load(startRowIndex, maximumRows, reader, out total));
return ds;
}
/// <summary>
/// 将datareader指定位置的记录加载到DataTable中
/// 遍历整个DataTable,以便取得total数目,内部将关闭datareader
/// 适合少量记录的分页处理
/// </summary>
/// <param name="startRowIndex"></param>
/// <param name="maximumRows"></param>
/// <param name="reader"></param>
/// <param name="total"></param>
/// <returns></returns>
public static DataTable Load(int startRowIndex, int maximumRows, IDataReader reader,out int total)
{
DataTable tb = new DataTable();
DataColumn col;
DataRow row;
int i;
for (i = 0; i < reader.FieldCount; i++)
{
col = new DataColumn();
col.ColumnName = reader.GetName(i);
col.DataType = reader.GetFieldType(i);
tb.Columns.Add(col);
}
total = 0;
int index = 0;
int hBound = startRowIndex + maximumRows;
using (reader)
{
while (reader.Read())
{
if (index >= startRowIndex && index < hBound)
{
row = tb.NewRow();
for (i = 0; i < reader.FieldCount; i++)
{
row[i] = reader[i];
}
tb.Rows.Add(row);
}
index++;
}
}
total = index;
return tb;
}