在WinForm中如何在选定一个单元格的时候让它自动选定一行  
我试了好久都没搞好!
求求高手啦!

解决方案 »

  1.   

    How can I select the entire row when the user clicks on a cell in the row?
    http://www.syncfusion.com/FAQ/WindowsForms/FAQ_c44c.aspx#q689q
      

  2.   

    id=int.Parse(this.SuperGrid1.SelectedItem .Cells[列数].Text.ToString().Trim());
      

  3.   

    DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
      

  4.   

    public class DataGrid:DataGridView
        {        public DataGrid()
            {
                this.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableWithoutHeaderText;
                this.MultiSelect = false;
                this.ShowCellToolTips = true;            this.AllowDrop = false;
                this.EditMode = DataGridViewEditMode.EditProgrammatically;
                this.AllowUserToAddRows = false;
                this.AllowUserToDeleteRows = false;
                this.AllowUserToOrderColumns = true;
                this.AllowUserToResizeColumns = true;
                this.AllowUserToResizeRows = true;            DataGridViewCellStyle loAlterStyle = new DataGridViewCellStyle();
                loAlterStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
                loAlterStyle.BackColor = Color.FromArgb(192, 255, 255);
                loAlterStyle.ForeColor = Color.Black;
                loAlterStyle.WrapMode = DataGridViewTriState.NotSet;
                this.AlternatingRowsDefaultCellStyle = loAlterStyle;            this.BackgroundColor = Color.LightYellow;
                this.BorderStyle = BorderStyle.FixedSingle;
                this.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
                this.CellBorderStyle = DataGridViewCellBorderStyle.SingleVertical;
                this.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single;
                this.ColumnHeadersVisible = true;
                this.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing;
                this.ColumnHeadersHeight = 20;
                DataGridViewCellStyle loHeaderStyle = new DataGridViewCellStyle();            loHeaderStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
                loHeaderStyle.BackColor = Color.FromArgb(192, 255, 255);
                loHeaderStyle.ForeColor = Color.Black;
                loHeaderStyle.SelectionBackColor = Color.FromArgb(0, 0, 64);
                loHeaderStyle.SelectionForeColor = Color.White;
                loAlterStyle.WrapMode = DataGridViewTriState.NotSet;
                this.ColumnHeadersDefaultCellStyle = loHeaderStyle;            this.GridColor = Color.FromArgb(128, 255, 255);
                this.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                this.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
                this.ReadOnly = true;
                this.EditMode = DataGridViewEditMode.EditOnF2;
                this.RowHeadersVisible = false;
                this.RowHeadersWidth = 5;
                //this.AlternatingRowsDefaultCellStyle.BackColor = Color.;
            }        protected override void OnRowPrePaint(DataGridViewRowPrePaintEventArgs e)
            {
                //去除选中Cell时的虚框
                e.PaintParts = e.PaintParts ^ DataGridViewPaintParts.Focus;
                base.OnRowPrePaint(e);
            }        protected override void OnCellMouseEnter(DataGridViewCellEventArgs e)
            {
                if (e.ColumnIndex > -1 && e.RowIndex > -1)
                {
                    string lcDataStr = this.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
                    this.Rows[e.RowIndex].Cells[e.ColumnIndex].ToolTipText = lcDataStr;
                }
                base.OnCellMouseEnter(e);
            }        protected override void OnCellMouseClick(DataGridViewCellMouseEventArgs e)
            {
                //右键点击时把当前内容拷贝到粘贴板
                if (e.Button == MouseButtons.Right)
                {
                    if (e.ColumnIndex > -1 && e.RowIndex > -1)
                    {
                        string lcDataStr = this.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
                        Clipboard.SetText(lcDataStr);
                    }
                }
                base.OnCellMouseClick(e);
            }
        }