您的位置:首页 > 编程语言 > ASP

asp.net GridView常用代码集锦

2012-07-02 20:04 232 查看
1、对GridView通用UpdateSQL生成函数,对单表有效

public bool GetUpdateRow(GridView gv, int RowIndex,out DALDataRow drRow,out DALDataRow drKeys)
{
    drRow = GetDataRow(gv, RowIndex);
    drKeys = GetDataKeys(gv, RowIndex);
    if (drRow == null||drKeys==null)
    {
        return false;
    }//if
    return true;
}
private DALDataRow GetDataKeys(GridView gv, int RowIndex)
{
    DALDataRow drKeys = new DALDataRow();
    var KeyNames = gv.DataKeyNames;
    var Keys = gv.DataKeys[RowIndex].Values;
    for( int i=0; i<KeyNames.Length;i++)
    {
        drKeys.Add(KeyNames[i].ToLower(), Keys[i].ToString());
    }//for( int i=
    return drKeys;
}
public DALDataRow GetDataRow(GridView gv,int RowIndex)
{
    List<string> DataFields = new List<string>();
    List<string> Values = new List<string>();
    DALDataRow dr = new DALDataRow();
    foreach (DataControlField c in gv.Columns)
    {
        if (c is BoundField)
        {
            DataFields.Add(((BoundField)c).DataField.ToString().ToLower());
        }
        if (c is TemplateField)
        {
            TemplateField t = c as TemplateField;
            
        }
    }
    foreach (TableCell r in gv.Rows[RowIndex].Cells)
    {
        if (r.Controls[0] is TextBox)
        {
            Values.Add(((TextBox)(r.Controls[0])).Text);
        }
    }
    if (DataFields.Count!=Values.Count
        ||DataFields.Count < 1)
    {
        return null;
    }//if
    for (int i = 0; i < DataFields.Count;i++ )
    {
        dr.Add(DataFields[i], Values[i].ToString());
    }//for
    return dr;
}


2、GridView每几行增加一空行的方法(转)

经常见到文章列表中,每隔五行或十行下面有一空行,这样当阅读网站内容时,会不觉得太压抑,其实这个GridView也可以做到。

//1.首先GridView要定义OnRowDataBound事件,例如:OnRowDataBound="gv_RowDataBound"
//2.完善gv_RowDataBound方法体内容:
protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
	if   (e.Row.RowIndex   > 0   &&     (e.Row.RowIndex+1)   %   5   ==   0)         
	{ 
		GridViewRow newRow = new GridViewRow(0,0,DataControlRowType.DataRow,DataControlRowState.Normal);     
		newRow.Cells.Add(new   TableCell());                      
		newRow.Cells[0].ColumnSpan   =   e.Row.Cells.Count;                    
		newRow.Cells[0].Text   =   "  ";                       
		this.gv.Controls[0].Controls.Add(newRow);             
	} 
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: