C# .NET - How to get the current Position on the DataGridView Control in Windows application?

Asked By Eswaran Radhakrishnan on 09-Oct-08 08:18 AM
Hi,

I am displaying records in datagridView control when the form is loaded in the Windows Application. After the form is loaded, the first record is selected in the datagridView automatically. I have a button also on the same form to delete a particular row. If I click on the second record on the datagridview and click the button i need to take the particular colum from the second row in the datagridview control.

How to select that?

Thanks
RE

Try this - ram kumar replied to Eswaran Radhakrishnan on 09-Oct-08 08:37 AM

Hi,

using System;
using System.Drawing;
using System.Windows.Forms;

class Form1 : Form
{
    [STAThreadAttribute()]
    public static void Main()
    {
        Application.Run(new Form1());
    }

    public Form1()
    {
        DataGridView dataGridView1 = new DataGridView();
        DataGridViewRolloverCellColumn col =
            new DataGridViewRolloverCellColumn();
        dataGridView1.Columns.Add(col);
        dataGridView1.Rows.Add(new string[] { "" });
        dataGridView1.Rows.Add(new string[] { "" });
        dataGridView1.Rows.Add(new string[] { "" });
        dataGridView1.Rows.Add(new string[] { "" });
        this.Controls.Add(dataGridView1);
        this.Text = "DataGridView rollover-cell demo";
    }
}

public class DataGridViewRolloverCell : DataGridViewTextBoxCell
{
    protected override void Paint(
        Graphics graphics,
        Rectangle clipBounds,
        Rectangle cellBounds,
        int rowIndex,
        DataGridViewElementStates cellState,
        object value,
        object formattedValue,
        string errorText,
        DataGridViewCellStyle cellStyle,
        DataGridViewAdvancedBorderStyle advancedBorderStyle,
        DataGridViewPaintParts paintParts)
    {
        // Call the base class method to paint the default cell appearance.
        base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState,
            value, formattedValue, errorText, cellStyle,
            advancedBorderStyle, paintParts);

        // Retrieve the client location of the mouse pointer.
        Point cursorPosition =
            this.DataGridView.PointToClient(Cursor.Position);

        // If the mouse pointer is over the current cell, draw a custom border.
        if (cellBounds.Contains(cursorPosition))
        {
            Rectangle newRect = new Rectangle(cellBounds.X + 1,
                cellBounds.Y + 1, cellBounds.Width - 4,
                cellBounds.Height - 4);
            graphics.DrawRectangle(Pens.Red, newRect);
        }
    }

    // Force the cell to repaint itself when the mouse pointer enters it.
    protected override void OnMouseEnter(int rowIndex)
    {
        this.DataGridView.InvalidateCell(this);
    }

    // Force the cell to repaint itself when the mouse pointer leaves it.
    protected override void OnMouseLeave(int rowIndex)
    {
        this.DataGridView.InvalidateCell(this);
    }

}

public class DataGridViewRolloverCellColumn : DataGridViewColumn
{
    public DataGridViewRolloverCellColumn()
    {
        this.CellTemplate = new DataGridViewRolloverCell();
    }
}

REply - Binny ch replied to Eswaran Radhakrishnan on 09-Oct-08 08:37 AM

The DataGridView has a GetCellDisplayRectangle method. Just pass it the column and row index, which you'll generally get from the 'e' parameter in a cell-related event handler

solution - Perry replied to Eswaran Radhakrishnan on 09-Oct-08 10:02 AM

Hi,
 
If I understand you correctly, you want to delete rows selected by a user from your DGV.

1. Use the DataGridViewRowCollection of your DGV rather than the DataRowCollection of the DataTable. The DataGridViewRow has the Selected property that indicates whether a row is selected or otherwise.

2. Once you have determined that a row is to be deleted, you can use the Remove method of the DataGridViewRowCollection to delete the item from the grid, e.g. YerDataGridView.Rows.Remove(row)

3. Note that at this point, although the item is removed from the DGV, it still has not been deleted from the Access DB. You need to call the TableAdapter Update method on your DataSet/DataTable to commit the deletions to the DB, e.g. YerTableAdapter.Update(YerDataSet)

I normally would call Update once to commit the changes only after having removed all the items to be deleted from the DGV.
 
Code to remove selected row:
 
if dgvTeachers.SelectedRows.count>0 {
         DataGridViewRow selRow = dgv.SelectedRows(0)
         dgv.Rows.Remove(selRow);
}
 
Regards,
Megha
examples - Perry replied to Eswaran Radhakrishnan on 09-Oct-08 10:04 AM

Hi,

See http://msdn.microsoft.com/en-us/library/x8x9zk5a.aspx for getting details about edit,update,delete rows, colunms,cells of gridview.

private void selectedRowsButton_Click(object sender, System.EventArgs e)
{
    Int32 selectedRowCount =
        dataGridView1.Rows.GetRowCount(DataGridViewElementStates.Selected);
    if (selectedRowCount > 0)
    {
        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        for (int i = 0; i < selectedRowCount; i++)
        {
            sb.Append("Row: ");
            sb.Append(dataGridView1.SelectedRows[i].Index.ToString());
            sb.Append(Environment.NewLine);
        }
        sb.Append("Total: " + selectedRowCount.ToString());
        MessageBox.Show(sb.ToString(), "Selected Rows");
    }
}
Regards,
Megha
try this.... - Vasanthakumar D replied to Eswaran Radhakrishnan on 11-Oct-08 02:46 AM

Hi,

try the below one....

use below code in delete button click

if(DataGridView1.SelectedRows.Count > 0)

{

string strId = "";

strId = DataGridView1.SelectedRows[0].Cells[cellIndex].Value.ToString();

//Code to delete

}