如何利用RadioButtonList实现datagrid列的单选

来源:互联网 发布:网络宣传与洞察力 编辑:程序博客网 时间:2024/04/29 17:57
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>

datagrid中,我们可能会需要实现这种功能——列的单选,本身datagrid提供了select命令可以实现这种功能,而我们往往又习惯于RadioButton来实现,下面就谈谈怎么样用RadioButtonList来实现

1、我们将datagrid的第一列设置为模板列,并加入RadioButtonList 

2、在datagrid.databind();后加入以下代码

//将第一列第一单元格的RowSpan设置为datagrid的总列数

datagrid.Items[0].Cells[0].RowSpan=datagrid.Items.Count;  
   for (int i=1;i<datagrid.Items.Count;++i)
   {
              datagrid.Items[i].Cells[0].Visible=false;  //从第二列开始隐藏第一个单元格
   }

//将第一列第一个单元格里的RadioButtonList按照datagrid的总列数进行列添加

   for (int i=0;i<datagrid.Items.Count;++i)
   {
                ListItem ss=new ListItem("","1");
    ((RadioButtonList)datagrid.Items[0].Cells[0].Controls[1]).Items.Add(ss);
   }

 

 

 

OK........

<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
原创粉丝点击