如何在WinForm中的DataGrid中加入单选列?郁闷55555

amethystbaby 2005-05-10 12:59:57
最好是加入一列RadioButton,或者其他能实现这个功能的也行,就是在DataGrid中的一列,实现可以单选一行的功能.我一开始继承了DataGridColumnStyle可是程序运行时不会显示RadioButton,单击时可以显示,但不能单选,最小化回来后又只剩最后一个了.实在不知道怎么办了?哪位大侠帮帮小妹吧?郁闷好几天了...555...
...全文
369 32 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
32 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaomatian 2005-05-12
  • 打赏
  • 举报
回复
给你个加ComBox的例子看看吧!
C#中为DataGrid添加下拉列表框
本文将介绍如何在 System.Windows.Forms.DataGrid中切入使用ComboBox控件,主要包括三方面的内容。

  1. 在DataGrid中加入ComboBox列;

  2. 把在DataGrid中的修改保存到对应的网格;
 
  3. 设置DataGrid中网格的焦点。

  下面是整个源代码,一些功能可以看注释。

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace DataGridTest
{
 public class Form1 : System.Windows.Forms.Form
 {
  private System.Windows.Forms.DataGrid dgdFunctionArea;
  private DataTable dtblFunctionalArea;
  private System.Windows.Forms.Button buttonFocus;
  private System.ComponentModel.Container components = null;

  public Form1()
  {
   InitializeComponent();
   PopulateGrid();
  }

  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }

  #region Windows 窗体设计器生成的代码

  private void InitializeComponent()
  {
   this.dgdFunctionArea = new System.Windows.Forms.DataGrid();
   this.buttonFocus = new System.Windows.Forms.Button();
   ((System.ComponentModel.ISupportInitialize)(this.dgdFunctionArea)).BeginInit();
   this.SuspendLayout();
   //
   // dgdFunctionArea
   //
   this.dgdFunctionArea.DataMember = "";
   this.dgdFunctionArea.HeaderForeColor = System.Drawing.SystemColors.ControlText;

   this.dgdFunctionArea.Location = new System.Drawing.Point(4, 8);
   this.dgdFunctionArea.Name = "dgdFunctionArea";
   this.dgdFunctionArea.Size = new System.Drawing.Size(316, 168);
   this.dgdFunctionArea.TabIndex = 0;
   //
   // buttonFocus
   //
   this.buttonFocus.Location = new System.Drawing.Point(232, 188);
   this.buttonFocus.Name = "buttonFocus";
   this.buttonFocus.Size = new System.Drawing.Size(84, 23);
   this.buttonFocus.TabIndex = 1;
   this.buttonFocus.Text = "获取焦点";
   this.buttonFocus.Click += new System.EventHandler(this.buttonFocus_Click);
   //
   // Form1
   //
   this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
   this.ClientSize = new System.Drawing.Size(332, 217);
   this.Controls.Add(this.buttonFocus);
   this.Controls.Add(this.dgdFunctionArea);
   this.Name = "Form1";
   this.Text = "Form1";
   ((System.ComponentModel.ISupportInitialize)(this.dgdFunctionArea)).EndInit();
   this.ResumeLayout(false);

  }
  #endregion
  /// <summary>
  /// 应用程序的主入口点。
  /// </summary>
  [STAThread]
  static void Main()
  {
   Application.Run(new Form1());
  }
  //初始化DataGrid
  private void PopulateGrid()
  {
   //创建一个DataTable对象,包括四列,前三列为String,最后一列为Boolean。
   dtblFunctionalArea = new DataTable ("FunctionArea");
   string[] arrstrFunctionalArea = new string [3]{"Functional Area","Min","Max"};
   DataColumn dtCol = null;
   //创建String列
   for(int i=0; i< 3;i++)
   {
    dtCol = new DataColumn(arrstrFunctionalArea[i]);
    dtCol.DataType = Type.GetType("System.String");
    dtCol.DefaultValue = "";
    dtblFunctionalArea.Columns.Add(dtCol);
   }

   //创建Boolean列,用CheckedBox来显示。
   DataColumn dtcCheck = new DataColumn("IsMandatory");
   dtcCheck.DataType = System.Type.GetType("System.Boolean");
   dtcCheck.DefaultValue = false;
   dtblFunctionalArea.Columns.Add(dtcCheck);

   //把表绑定到DataGrid
   dgdFunctionArea.DataSource = dtblFunctionalArea;

   //为DataGrid加载DataGridTableStyle样式
   if(!dgdFunctionArea.TableStyles.Contains("FunctionArea"))
   {
    DataGridTableStyle dgdtblStyle = new DataGridTableStyle();
    dgdtblStyle.MappingName = dtblFunctionalArea.TableName;
    dgdFunctionArea.TableStyles.Add(dgdtblStyle);
    dgdtblStyle.RowHeadersVisible = false;
    dgdtblStyle.HeaderBackColor = Color.LightSteelBlue;
    dgdtblStyle.AllowSorting = false;
    dgdtblStyle.HeaderBackColor = Color.FromArgb(8,36,107);
    dgdtblStyle.RowHeadersVisible = false;
    dgdtblStyle.HeaderForeColor = Color.White;
    dgdtblStyle.HeaderFont = new System.Drawing.Font("Microsoft Sans Serif", 9F,
    System.Drawing.FontStyle.Bold,
    System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
    dgdtblStyle.GridLineColor = Color.DarkGray;
    dgdtblStyle.PreferredRowHeight = 22;
    dgdFunctionArea.BackgroundColor = Color.White;

    //设置列的宽度
    GridColumnStylesCollection colStyle = dgdFunctionArea.TableStyles[0].GridColumnStyles;
    colStyle[0].Width = 100;
    colStyle[1].Width = 50;
    colStyle[2].Width = 50;
    colStyle[3].Width = 80;
   }

   DataGridTextBoxColumn dgtb = (DataGridTextBoxColumn)dgdFunctionArea.TableStyles[0].GridColumnStyles[0];

   ComboBox cmbFunctionArea = new ComboBox();
   cmbFunctionArea.Items.AddRange(new object[]{"选项一","选项二","选项三"});
   cmbFunctionArea.Cursor = Cursors.Arrow;
   cmbFunctionArea.DropDownStyle= ComboBoxStyle.DropDownList;
   cmbFunctionArea.Dock = DockStyle.Fill;

   //在选定项发生更改并且提交了该更改后发生

   cmbFunctionArea.SelectionChangeCommitted += new  EventHandler(cmbFunctionArea_SelectionChangeCommitted);

   //把ComboBox添加到DataGridTableStyle的第一列

   dgtb.TextBox.Controls.Add(cmbFunctionArea);

  }

  //设置焦点模拟

  private void GetFocus(int row,int col)
  {
   //先把焦点移动到DataGrid
   this.dgdFunctionArea.Focus();
   //把焦点移动到DataGridCell
   DataGridCell dgc = new DataGridCell(row,col);
   this.dgdFunctionArea.CurrentCell = dgc;
   DataGridTextBoxColumn dgtb = (DataGridTextBoxColumn)dgdFunctionArea.TableStyles[0].GridColumnStyles[col];

   //设置焦点

   dgtb.TextBox.Focus();
  }

  //把Combobox上修改的数据提交到当前的网格

 private void cmbFunctionArea_SelectionChangeCommitted(object sender, EventArgs e)
 {
  this.dgdFunctionArea[this.dgdFunctionArea.CurrentCell] = ((ComboBox)sender).SelectedItem.ToString();

 }

 //设置新的焦点

 private void buttonFocus_Click(object sender, System.EventArgs e)
 {
  //焦点模拟,这里设置第三行第一列
  GetFocus(2,0);
 }
}

}

amethystbaby 2005-05-12
  • 打赏
  • 举报
回复
谢谢大家!^_^
To:xiaomatian
谢谢,这个代码我有的,不过没用过.嘿嘿...
再次谢谢大家
njuhuangmy 2005-05-12
  • 打赏
  • 举报
回复
在现在的一个项目里, 为了实现只选中DataGrid的一行这个功能

我重载了 DataGrid控件的MouseDown, MouseUp, 还有 Scroll

现在能保证 鼠标操作, 鼠标+ctrl , 鼠标+shift 操作, 最后都返回选择某一行,

或者没有行被选中..

还没能实现 键盘的操作..保证单行被选中 ...
amethystbaby 2005-05-11
  • 打赏
  • 举报
回复
To:loveboy_3()
1.cboPartNumber是不是要加的东西?
2.这是不是要在DataGrid后面人为加一列啊?
loveboy_3 2005-05-11
  • 打赏
  • 举报
回复
单选的话按照 fangxinggood(JustACoder)的说法就可以。加其他的Control,用下面的方法实现
if (this.dataGrid1.CurrentCell.ColumnNumber==1)
{
//this.cboPartNumber.Bounds=this.dataGrid1.GetCurrentCellBounds();
this.cboPartNumber.Left=this.dataGrid1.GetCurrentCellBounds().Left + this.dataGrid1.Left;
this.cboPartNumber.Top=this.dataGrid1.GetCurrentCellBounds().Top + this.dataGrid1.Top;
this.cboPartNumber.Width=this.dataGrid1.GetCurrentCellBounds().Width;
this.cboPartNumber.Height=this.dataGrid1.GetCurrentCellBounds().Height;
this.cboPartNumber.Visible=true;
this.cboPartNumber.BringToFront();
this.cboPartNumber.Text=this.dataGrid1[this.dataGrid1.CurrentCell].ToString();
}
else
{
this.cboPartNumber.Visible=false;
}
amethystbaby 2005-05-11
  • 打赏
  • 举报
回复
大家帮帮忙啊!
amethystbaby 2005-05-10
  • 打赏
  • 举报
回复
没有更好的办法么?
amethystbaby 2005-05-10
  • 打赏
  • 举报
回复
To:lyvvvv()
这不就是DataGridBoolColumn自带的功能么?好像不能实现单选啊?还是同时有几个checked(打勾)了啊...
lyvvvv 2005-05-10
  • 打赏
  • 举报
回复
加入一个DataGridBoolColumn列,查询中给这列一个空值,比如:
sSql[0] = "select '0' as ok,a.* from ZD_SFXM a order by a.SFLB_CODE,a.SFXM_CODE";
其中OK就是该列,然后写如下代码:
private void dataGrid1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
DataGrid myGrid = (DataGrid) sender;
if(myGrid.VisibleColumnCount == 0 || myGrid.CurrentRowIndex <0)return;
if (myGrid[myGrid.CurrentRowIndex,0].ToString().ToLower()=="true")
myGrid[myGrid.CurrentRowIndex,0]=false;
else
myGrid[myGrid.CurrentRowIndex,0]=true;
myGrid.Select(myGrid.CurrentRowIndex);
}
机器人 2005-05-10
  • 打赏
  • 举报
回复
也是,那么,加个CheckBox也是可以的。
amethystbaby 2005-05-10
  • 打赏
  • 举报
回复
嗯.这样可以的...就是人性化不太好,最后能提供按钮或其他控件,否则用户怎么知道点哪里呢?是不?呵呵...要求好像有点高...嘿嘿...
机器人 2005-05-10
  • 打赏
  • 举报
回复
不知道怎么才能添加一列别的东东..:(
在Datagrid的数据源Datatable加上一列,就可以了。我上面的例子是点一个单元格就自动添加一行的。
机器人 2005-05-10
  • 打赏
  • 举报
回复
为啥不用RowHeader ?
amethystbaby 2005-05-10
  • 打赏
  • 举报
回复
没事,谢谢你呢...呵呵...又学了一招...不知道怎么才能添加一列别的东东..:(
机器人 2005-05-10
  • 打赏
  • 举报
回复
恩,你说的没错,的确没有变化,除非点击行头。我再看看。(有点想当然了,对不起)
amethystbaby 2005-05-10
  • 打赏
  • 举报
回复
嗯.....其实...偶想实现的是后面有个框什么的,可以点一下表示要单选....最好是RadioButton.如果不行.CheckBox也行.那位(whyxx(java?.net?我们要学会用两条腿走路)大哥,还没发给我代码:(
最后单击行来选也行.不过那个myGrid.Select(row)为什么没什么变化?我以为应该是整个行变色什么的,表示选中了?
呵呵...谢谢大家哈!~
机器人 2005-05-10
  • 打赏
  • 举报
回复
写完整点吧。

private void dataGrid1_MouseDown(object sender, MouseEventArgs e)
{
DataGrid myGrid = (DataGrid) sender;
DataGrid.HitTestInfo hitInfo = myGrid.HitTest(e.X, e.Y);
int col = hitInfo.Column;
int row = hitInfo.Row;
if( hitInfo.Type == DataGrid.HitTestType.Cell )
{
myGrid.Select(row);
}
}

机器人 2005-05-10
  • 打赏
  • 举报
回复
获得点击单元格的行号,剩下的...

调用DataGrid.Select( row )方法。
amethystbaby 2005-05-10
  • 打赏
  • 举报
回复
To:fangxinggood()
我想实现单选呢?这段是获得点击了哪个单元吗?好像不太一样啊....
conan19771130 2005-05-10
  • 打赏
  • 举报
回复
datagrid[行][列]
加载更多回复(12)

111,105

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧