DataGrid 完全攻略之一(在编辑状态下改变控件的显示大小)

简介:

页面代码:html
<%@ Page language="c#" Codebehind="AdjustWidth.aspx.cs" AutoEventWireup="false" Inherits="MsDataGrid.AdjustWidth" %> <%@ Page language="c#" Codebehind="AdjustWidth.aspx.cs" AutoEventWireup="false" Inherits="MsDataGrid.AdjustWidth" %>  
ExpandedBlockStart.gif <% @ Page language="c#" Codebehind="AdjustWidth.aspx.cs" AutoEventWireup="false" Inherits="MsDataGrid.AdjustWidth"  %>
None.gif
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"  >
None.gif
< HTML >
None.gif    
< HEAD >
None.gif        
< title > DataGrid使用举例 </ title >
None.gif        
< meta  name ="GENERATOR"  Content ="Microsoft Visual Studio 7.0" >
None.gif        
< meta  name ="CODE_LANGUAGE"  Content ="C#" >
None.gif        
< meta  name ="vs_defaultClientScript"  content ="JavaScript" >
None.gif        
< meta  name ="vs_targetSchema"  content ="http://schemas.microsoft.com/intellisense/ie5" >
None.gif    
</ HEAD >
None.gif    
< body  MS_POSITIONING ="GridLayout" >
None.gif        
< form  id ="Form1"  method ="post"  runat ="server" >
None.gif            
< FONT  face ="宋体" >
None.gif                
< asp:DataGrid  id ="dgShow"  style ="Z-INDEX: 101; LEFT: 31px; POSITION: absolute; TOP: 93px"  runat ="server"  Width ="842px"  Height ="172px"  BorderColor ="Tan"  BorderWidth ="1px"  BackColor ="LightGoldenrodYellow"  CellPadding ="2"  GridLines ="None"  ForeColor ="Black"  PageSize ="1"  AutoGenerateColumns ="False" >
None.gif                    
< SelectedItemStyle  ForeColor ="GhostWhite"  BackColor ="DarkSlateBlue" ></ SelectedItemStyle >
None.gif                    
< AlternatingItemStyle  BackColor ="PaleGoldenrod" ></ AlternatingItemStyle >
None.gif                    
< HeaderStyle  Font-Bold ="True"  BackColor ="Tan" ></ HeaderStyle >
None.gif                    
< FooterStyle  BackColor ="Tan" ></ FooterStyle >
None.gif                    
< Columns >
None.gif                        
< asp:BoundColumn  DataField ="StudentID"  ReadOnly ="True"  HeaderText ="学生ID" ></ asp:BoundColumn >
None.gif                        
< asp:BoundColumn  DataField ="StudentName"  HeaderText ="学生姓名" ></ asp:BoundColumn >
None.gif                        
< asp:BoundColumn  DataField ="StudentPass"  HeaderText ="密码" ></ asp:BoundColumn >
None.gif                        
< asp:BoundColumn  DataField ="Sex"  HeaderText ="性别" ></ asp:BoundColumn >
None.gif                        
< asp:BoundColumn  DataField ="Birthday"  HeaderText ="生日"  DataFormatString ="{0:yyyy-M-d}" ></ asp:BoundColumn >
None.gif                        
< asp:BoundColumn  DataField ="Email"  HeaderText ="邮件地址" ></ asp:BoundColumn >
None.gif                        
< asp:TemplateColumn  HeaderText ="性别模板列" >
None.gif                            
< ItemTemplate >
None.gif                                
< asp:RadioButton  id =RadioButton2  runat ="server"  Text ="男"  Checked ='<%#  DataBinder.Eval(Container, "DataItem.Sex") % > ' Enabled="False">
None.gif                                
</ asp:RadioButton >
None.gif                                
< asp:RadioButton  id =RadioButton1  runat ="server"  Text ="女"  Checked ='<%#  !(bool)DataBinder.Eval(Container, "DataItem.Sex") % > ' Enabled="False">
None.gif                                
</ asp:RadioButton >
None.gif                            
</ ItemTemplate >
None.gif                            
< EditItemTemplate >
None.gif                                
< asp:RadioButton  id =cbSex  runat ="server"  Text ="男"  Checked ='<%#  DataBinder.Eval(Container, "DataItem.Sex") % > ' GroupName="Sex">
None.gif                                
</ asp:RadioButton >
None.gif                                
< asp:RadioButton  id =RadioButton4  runat ="server"  Text ="女"  Checked ='<%#  !(bool)DataBinder.Eval(Container, "DataItem.Sex") % > ' GroupName="Sex">
None.gif                                
</ asp:RadioButton >
None.gif                            
</ EditItemTemplate >
None.gif                        
</ asp:TemplateColumn >
None.gif                        
< asp:ButtonColumn  Text ="选择"  HeaderText ="选择"  CommandName ="Select" ></ asp:ButtonColumn >
None.gif                        
< asp:EditCommandColumn  ButtonType ="LinkButton"  UpdateText ="更新"  HeaderText ="操作"  CancelText ="取消"  EditText ="编辑" ></ asp:EditCommandColumn >
None.gif                        
< asp:ButtonColumn  Text ="删除"  HeaderText ="删除"  CommandName ="Delete" ></ asp:ButtonColumn >
None.gif                        
< asp:HyperLinkColumn  Text ="点击查看"  DataNavigateUrlField ="StudentID"  DataNavigateUrlFormatString ="Show.aspx?ID={0}"  DataTextField ="StudentName"  HeaderText ="详细信息" ></ asp:HyperLinkColumn >
None.gif                    
</ Columns >
None.gif                    
< PagerStyle  HorizontalAlign ="Center"  ForeColor ="DarkSlateBlue"  BackColor ="PaleGoldenrod" ></ PagerStyle >
None.gif                
</ asp:DataGrid ></ FONT >
None.gif        
</ form >
None.gif    
</ body >
None.gif
</ HTML >
None.gif
后台代码:cs
None.gif using  System;
None.gif
using  System.Collections;
None.gif
using  System.ComponentModel;
None.gif
using  System.Data;
None.gif
using  System.Drawing;
None.gif
using  System.Web;
None.gif
using  System.Web.SessionState;
None.gif
using  System.Web.UI;
None.gif
using  System.Web.UI.WebControls;
None.gif
using  System.Web.UI.HtmlControls;
None.gif
using  System.Data.SqlClient;
None.gif
namespace  MsDataGrid
ExpandedBlockStart.gif
{
ExpandedSubBlockStart.gif    
/// <summary>
InBlock.gif    
/// WebForm1 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class AdjustWidth : System.Web.UI.Page
ExpandedSubBlockStart.gif    
{
InBlock.gif        
protected System.Web.UI.WebControls.DataGrid dgShow;
InBlock.gif    
InBlock.gif        
private void Page_Load(object sender, System.EventArgs e)
ExpandedSubBlockStart.gif        
{
InBlock.gif            
// 在此处放置用户代码以初始化页面
InBlock.gif
            if(!IsPostBack)
InBlock.gif                BindData();
InBlock.gif            
ExpandedSubBlockEnd.gif        }

InBlock.gif        
private void BindData()
ExpandedSubBlockStart.gif        
{
InBlock.gif            
string strCon = System.Configuration.ConfigurationSettings.AppSettings["DSN"];
InBlock.gif            SqlConnection con 
= new SqlConnection(strCon);
InBlock.gif            SqlDataAdapter da 
= new SqlDataAdapter("Select * from tbStudentinfo",con);
InBlock.gif            DataSet ds 
= new DataSet();
InBlock.gif            da.Fill(ds,
"studentinfo");
InBlock.gif            dgShow.DataSource 
= ds.Tables["studentinfo"].DefaultView;
InBlock.gif            dgShow.DataBind();
InBlock.gif            dgShow.Columns[
0].Visible = false;
InBlock.gif            
ExpandedSubBlockEnd.gif        }

ContractedSubBlock.gif        
Web Form Designer generated code
InBlock.gif
InBlock.gif        
private void dgShow_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
ExpandedSubBlockStart.gif        
{
InBlock.gif            dgShow.EditItemIndex 
= e.Item.ItemIndex;
InBlock.gif            BindData();
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif    
InBlock.gif        
private void dgShow_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
ExpandedSubBlockStart.gif        
{
InBlock.gif            dgShow.EditItemIndex 
= -1;
InBlock.gif            BindData();
ExpandedSubBlockEnd.gif        }

InBlock.gif        
private void dgShow_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
ExpandedSubBlockStart.gif        
{
InBlock.gif            dgShow.CurrentPageIndex 
= e.NewPageIndex;
InBlock.gif            BindData();
ExpandedSubBlockEnd.gif        }

InBlock.gif        
private void dgShow_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
ExpandedSubBlockStart.gif        
{
InBlock.gif            
if(dgShow.Items.Count==1)
ExpandedSubBlockStart.gif            
{
InBlock.gif                
if(dgShow.CurrentPageIndex!=0)
InBlock.gif                    dgShow.CurrentPageIndex 
= dgShow.CurrentPageIndex-1;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
string strSql = "delete from tbStudentinfo where studentid="+e.Item.Cells[0].Text+"";
InBlock.gif            ExecuteSql(strSql);
InBlock.gif            BindData();
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gif        
////////////////////////////////////////////////////////////
InBlock.gif        //说明:执行制定SQL语句/////////////////////////////////////
ExpandedSubBlockStart.gif
        ///////////////////////////////////////////////////////////
InBlock.gif        private void ExecuteSql(string strSql)
ExpandedSubBlockStart.gif        
{
InBlock.gif            
try
ExpandedSubBlockStart.gif            
{
InBlock.gif                
string strconn = System.Configuration.ConfigurationSettings.AppSettings["DSN"];//从Web.config中读取
InBlock.gif
                SqlConnection conn =new SqlConnection(strconn);
InBlock.gif                SqlCommand com 
= new SqlCommand(strSql,conn);
InBlock.gif                conn.Open();
InBlock.gif                com.ExecuteNonQuery();
InBlock.gif                conn.Close();
ExpandedSubBlockEnd.gif            }
 
InBlock.gif            
catch(Exception e)
ExpandedSubBlockStart.gif            
{
InBlock.gif                Response.Write(
"<script language = 'javascript'>alert('"+e.Message+"');</script>") ;
InBlock.gif                            
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
private void dgShow_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
ExpandedSubBlockStart.gif        
{
InBlock.gif            
string strStudentID = e.Item.Cells[0].Text;//处于非编辑状态
InBlock.gif
            string strName = ((TextBox)(e.Item.Cells[1].Controls[0])).Text;//处于编辑状态
InBlock.gif
            string strPass =((TextBox)(e.Item.Cells[2].Controls[0])).Text;
InBlock.gif            
string strSex = ((CheckBox)(e.Item.Cells[3].FindControl("cbSex"))).Checked?"1":"0";
InBlock.gif            
string strBirthday =((TextBox)(e.Item.Cells[4].Controls[0])).Text;
InBlock.gif            
string strEmail =((TextBox)(e.Item.Cells[5].Controls[0])).Text;
InBlock.gif            
string strSql = "update tbStudentinfo set StudentName='"+strName+"',StudentPass='"+strPass+"'";
InBlock.gif            strSql 
+=",Sex="+strSex+",Birthday='"+strBirthday+"',Email='"+strEmail+"' where studentid="+strStudentID+"";
InBlock.gif            ExecuteSql(strSql);
InBlock.gif            dgShow.EditItemIndex 
= -1;
InBlock.gif            BindData();
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void dgShow_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
ExpandedSubBlockStart.gif        
{
InBlock.gif            
if (e.Item.ItemType == ListItemType.EditItem) 
ExpandedSubBlockStart.gif            
{
InBlock.gif                
for (int i=0;i<e.Item.Cells.Count;i++)
ExpandedSubBlockStart.gif                
{
InBlock.gif                    
if(e.Item.Cells[i].Controls.Count>0)
ExpandedSubBlockStart.gif                    
{
InBlock.gif                        
try
ExpandedSubBlockStart.gif                        
{
InBlock.gif                            TextBox t 
=(TextBox)e.Item.Cells[i].Controls[0];
InBlock.gif                            t.Width
=130;
ExpandedSubBlockEnd.gif                        }

InBlock.gif                        
catch(Exception ee)
ExpandedSubBlockStart.gif                        
{
ExpandedSubBlockEnd.gif                        }

ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}



本文转自高海东博客园博客,原文链接:http://www.cnblogs.com/ghd258/archive/2005/10/12/253175.html,如需转载请自行联系原作者
相关文章
|
2月前
|
前端开发 JavaScript 程序员
avue中怎样隐藏新增和编辑的按钮
avue中怎样隐藏新增和编辑的按钮
32 0
|
6月前
|
前端开发
uniapp checkbox样式失效,选中框选中按钮不显示
uniapp checkbox样式失效,选中框选中按钮不显示
86 0
|
9月前
|
前端开发
添加按钮的两种方式
添加按钮的两种方式
45 0
饿了么UI按钮禁用时加文字提示,按钮正常时不加文字提示(el-tooltip使用注意事项)
饿了么UI按钮禁用时加文字提示,按钮正常时不加文字提示(el-tooltip使用注意事项)
208 0
WPF中如何获取选中行/单元格所在行的数据
WPF中如何获取选中行/单元格所在行的数据
|
前端开发
前端工作小结75-修改表头按钮样式
前端工作小结75-修改表头按钮样式
72 0
前端工作小结75-修改表头按钮样式
|
存储
PyQt5 技巧篇-复选框绑定行内容,全选、清空、展示选中的内容功能实现演示,设置复选框选中,检查复选框选中状态
PyQt5 技巧篇-复选框绑定行内容,全选、清空、展示选中的内容功能实现演示,设置复选框选中,检查复选框选中状态
436 0
PyQt5 技巧篇-复选框绑定行内容,全选、清空、展示选中的内容功能实现演示,设置复选框选中,检查复选框选中状态
|
C# 前端开发
wpf中的datagrid绑定操作按钮是否显示或者隐藏
如图,需要在wpf中的datagrid的操作那列有个确认按钮,然后在某些条件下确认按钮可见,某些情况下不可见的,放在mvc里直接在cshtml页面中if..else就行了。 但是在wpf里不行。。网上搜索了好久才找到解决方法,原来只是binding那个visiable属性就行了,
6850 0