Solved

selected rows in winform gridview

Posted on 2009-07-13
4
641 Views
Last Modified: 2013-12-17
I have a gridview created in windows forms. I need to obtain the selected rows in the grid view, and clear the contents of column with index 3.
That is, the values of cells in column 3 in selected rows should be made "".

Should I use the SelectedRow property for this purpose? If yes, how?
0
Comment
Question by:Angel02
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 19

Expert Comment

by:daveamour
ID: 24847370
Hi
Do you mean a DataGridView rather than a Gridview?
0
 
LVL 19

Accepted Solution

by:
daveamour earned 500 total points
ID: 24847444
Anyway try something like this...
namespace EEGridView
{
    partial class Mainform
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;
 
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }
 
        #region Windows Form Designer generated code
 
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.testGrid = new System.Windows.Forms.DataGridView();
            this.TestButton = new System.Windows.Forms.Button();
            ((System.ComponentModel.ISupportInitialize)(this.testGrid)).BeginInit();
            this.SuspendLayout();
            // 
            // testGrid
            // 
            this.testGrid.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.testGrid.Location = new System.Drawing.Point(37, 30);
            this.testGrid.Name = "testGrid";
            this.testGrid.Size = new System.Drawing.Size(574, 331);
            this.testGrid.TabIndex = 0;
            // 
            // TestButton
            // 
            this.TestButton.Location = new System.Drawing.Point(37, 383);
            this.TestButton.Name = "TestButton";
            this.TestButton.Size = new System.Drawing.Size(75, 23);
            this.TestButton.TabIndex = 1;
            this.TestButton.Text = "Test";
            this.TestButton.UseVisualStyleBackColor = true;
            this.TestButton.Click += new System.EventHandler(this.TestButton_Click);
            // 
            // Mainform
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(653, 433);
            this.Controls.Add(this.TestButton);
            this.Controls.Add(this.testGrid);
            this.Name = "Mainform";
            this.Text = "Main Form";
            this.Load += new System.EventHandler(this.Mainform_Load);
            ((System.ComponentModel.ISupportInitialize)(this.testGrid)).EndInit();
            this.ResumeLayout(false);
 
        }
 
        #endregion
 
        private System.Windows.Forms.DataGridView testGrid;
        private System.Windows.Forms.Button TestButton;
    }
}
 
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
 
namespace EEGridView
{
    public partial class Mainform : Form
    {
        public Mainform()
        {
            InitializeComponent();
        }
 
        private void Mainform_Load(object sender, EventArgs e)
        {
            LoadData();
        }
 
        private void LoadData()
        {
            testGrid.DataSource = TestData();
        }
 
        private List<Person> TestData()
        {
            List<Person> people = new List<Person>();
 
            people.Add(new Person("Dave", "Amour", "Mansfield"));
            people.Add(new Person("Fred", "Bloggs", "Cannock"));
            people.Add(new Person("Tracey", "Smith", "Derby"));
 
            return people;
        }
 
        private void TestButton_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow row in testGrid.SelectedRows)
            {
                row.Cells[2].Value = string.Empty;
            }            
        }
    }
 
    public class Person
    {
        public Person(string firstname, string lastname, string town)
        {
            FirstName = firstname;
            LastName = lastname;
            Town = town;
        }
 
        public string FirstName
        {
            get;
 
            set;
        }
 
        public string LastName
        {
            get;
 
            set;
        }
 
        public string Town
        {
            get;
 
            set;
        }
    }
}

Open in new window

0
 

Author Comment

by:Angel02
ID: 24850414
Perfect! Thanks a lot. This is what I needed.

 private void TestButton_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow row in testGrid.SelectedRows)
            {
                row.Cells[2].Value = string.Empty;
            }            
        }

PS:
I think in Windows Forms its called DataGridView (System.Windows.Forms.DataGridView), and in web controls its called GridView (System.Web.UI.WebControls)
0
 
LVL 19

Expert Comment

by:daveamour
ID: 24852000
Yep thats right about the names.
To make it even more confusing they have different names in earlier versions of the framework too!
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question