DataGrid 完全攻略之六(使用RadioButton)

简介:

前台代码:html
ExpandedBlockStart.gif <% @ Page language="c#" Codebehind="BasicUse.aspx.cs" AutoEventWireup="false" Inherits="MsDataGrid.WebForm1"  %>
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 ="2"  AutoGenerateColumns ="False"  AllowPaging ="True" >
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"  Enabled ="False"  Checked ='<%#  DataBinder.Eval(Container, "DataItem.Sex") % > ' Text="男">
None.gif                                
</ asp:RadioButton >
None.gif                                
< asp:RadioButton  id =RadioButton1  runat ="server"  Enabled ="False"  Checked ='<%#  !(bool)DataBinder.Eval(Container, "DataItem.Sex") % > ' Text="女">
None.gif                                
</ asp:RadioButton >
None.gif                            
</ ItemTemplate >
None.gif                            
< EditItemTemplate >
None.gif                                
< asp:RadioButton  id =cbSex  runat ="server"  Checked ='<%#  DataBinder.Eval(Container, "DataItem.Sex") % > ' Text="男" GroupName="Sex">
None.gif                                
</ asp:RadioButton >
None.gif                                
< asp:RadioButton  id =RadioButton4  runat ="server"  Checked ='<%#  !(bool)DataBinder.Eval(Container, "DataItem.Sex") % > ' Text="女" 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 >

后台代码: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 WebForm1 : 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            
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
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif



本文转自高海东博客园博客,原文链接:http://www.cnblogs.com/ghd258/archive/2005/10/12/253205.html如需转载请自行联系原作者
相关文章
|
4天前
|
XML Java 数据格式
Anrodi Studio App开发入门之网格视图GridView的使用及实战(附源码 超详细必看)
Anrodi Studio App开发入门之网格视图GridView的使用及实战(附源码 超详细必看)
34 0
零基础VB教程040期:combobox下拉框如何灵活运用?
零基础VB教程040期:combobox下拉框如何灵活运用?
114 0
|
索引
VB基础:掌握listbox和combobox控件的应用【VB学习笔记2020课堂版04】
VB基础:掌握listbox和combobox控件的应用【VB学习笔记2020课堂版04】
142 0
VB基础:掌握listbox和combobox控件的应用【VB学习笔记2020课堂版04】
|
C#
WPF 自定义模板 Button闪亮效果
原文:WPF 自定义模板 Button闪亮效果 Button的选中Effect,我们看下下面的效果:   让我们再放大一点: 怎么设置上面样式呢?直接设置Button的Effect,有点问题,因为Effect不是四周环绕的。
567 0
|
C#
WPF 4 DataGrid 控件(进阶篇一)
原文:WPF 4 DataGrid 控件(进阶篇一)      上一篇《WPF 4 DataGrid 控件(自定义样式篇)》中,我们掌握了DataGrid 列表头、行表头、行、单元格相关的自定义样式方法,本篇我们继续对自定义样式进行一些高级设置。
1005 0
|
C#
WPF 4 DataGrid 控件(进阶篇二)
原文:WPF 4 DataGrid 控件(进阶篇二)      上一篇《WPF 4 DataGrid 控件(进阶篇一)》中我们通过DataGridTemplateColumn 类自定义编辑了日期列的样式,当然也可以根据个人需要设置任何样式模板。
1045 0
|
开发框架 移动开发