.net datagrid的使用

来源:互联网 发布:未来社交网络 编辑:程序博客网 时间:2024/05/17 18:44

asp.net中DataGrid双行跨列表头设计心得!
一、DataGrid的属性设置
  1. AllowPaging: true
  2. PageStyle->Position: TopAndBottom
3. 可选:PageStyle->HorizonAlign: Center(使文本居中显示)
  4. 可选:ItemStyle->HorizonAlign: Center(使文本居中显示)
二、代码部分
  1.首先,使DataGrid绑定数据库中某个表,例如:
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!IsPostBack)
{
SqlConnection myConn=new SqlConnection("server=localhost;uid=sa;pwd=sa;database=db_test");
SqlDataAdapter da=new SqlDataAdapter("Select * from 个人",myConn);
DataSet ds=new DataSet();
da.Fill(ds,"gr");
dgGeRen.DataSource=ds.Tables["gr"].DefaultView;
dgGeRen.DataBind();


}
  2.为DataGrid添加ItemCreated事件的处理函数、
  3.为了判断DataGrid中的两个(上下)Pager的位置,我们可以使用一个全局变量来判断。
定义一个全局变量 private int m_CreatePageTimes = 0;
  4.为DataGrid的ItemCreated事件的处理函数添加内容,如下:
private void dgGeRen_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
switch(e.Item.ItemType)
{
//case(ListItemType.Pager):
case ListItemType.Pager:
{
if(m_CreatePageTimes == 0)
{
DataGridItem row=(DataGridItem)e.Item;
row.Cells.Clear();

//row.BackColor=Color.Navy; //背景色
//row.ForeColor=Color.Red; //前景色

row.HorizontalAlign=HorizontalAlign.Center;//使文本居中显示

TableCell cell0=new TableCell();
cell0.RowSpan=2;
cell0.Controls.Add(new LiteralControl("姓名"));

TableCell cell1=new TableCell();
cell1.ColumnSpan=2;  //默认的ColumnSpan值为1
cell1.Text="住房地址信息";

//也可如此:cell1.Controls.Add(new LiteralControl("住房地址信息"));

//TableCell cell2=new TableCell();
//cell2.Controls.Add(new LiteralControl(""));
TableCell cell2=new TableCell();
cell2.RowSpan=2;
cell2.Text="出生日期";

row.Cells.Add(cell0);
row.Cells.Add(cell1);
row.Cells.Add(cell2);


m_CreatePageTimes++;
}
break;
}
case ListItemType.Header:
{
DataGridItem head=(DataGridItem)e.Item;
head.Cells.Clear();

//head.VerticalAlign=VerticalAlign.Middle;
//head.HorizontalAlign=HorizontalAlign.Center;

//TableCell cell00=new TableCell();
//cell00.RowSpan=2;
//cell00.Text="姓名";

TableCell cell01=new TableCell();
cell01.Text="楼号";

TableCell cell02=new TableCell();
cell02.Text="房号";

//TableCell cell03=new TableCell();
//cell03.Text="出生日期";

//head.Cells.Add(cell00);
head.Cells.Add(cell01);
head.Cells.Add(cell02);
//head.Cells.Add(cell03);
break;
}

}

}

屏蔽CTRL-V


在WinForm中的TextBox控件没有办法屏蔽CTRL-V的剪贴板粘贴动作,如果需要一个输入框,但是不希望用户粘贴剪贴板的内容,可以改用RichTextBox控件,并且在KeyDown中屏蔽掉CTRL-V键,例子:

private void richTextBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
if(e.Control & e.KeyCode==Keys.V)
e.Handled = true;

--------------------------------------------------------------------------------
 
 Panel 横向滚动,纵向自动扩展
<asp:panel style="overflow-x:scroll;overflow-y:auto;"></asp:panel>

回车转换成Tab

<script language="javascript" for="document" event="onkeydown">
 if(event.keyCode==13 & event.srcElement.type!=’button’ & event.srcElement.type!=’submit’& event.srcElement.type!=’reset’ & event.srcElement.type!=’’& event.srcElement.type!=’textarea’);
   event.keyCode=9;
</script>

onkeydown="if(event.keyCode==13) event.keyCode=9"

DataGrid行随鼠标变色

private void DGzf_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
 if (e.Item.ItemType!=ListItemType.Header)
 {
  e.Item.Attributes.Add( "onmouseout","this.style.backgroundColor=/""+e.Item.Style["BACKGROUND-COLOR"]+"/"");
  e.Item.Attributes.Add( "onmouseover","this.style.backgroundColor=/""+ "#EFF3F7"+"/"");
 }
}