开发者

Get the selected row in DataGridViewRow event in C#

开发者 https://www.devze.com 2023-04-07 17:21 出处:网络
I am using the DataGridView bound to a database. I have a button that is disabled.When a row is selected, not by click开发者_如何学Going in a cell but on the row selection pane, I want to respond to a

I am using the DataGridView bound to a database. I have a button that is disabled. When a row is selected, not by click开发者_如何学Going in a cell but on the row selection pane, I want to respond to an event and enable that button.


Well, there's the RowHeaderMouseClick event. From there, you can get e.RowIndex to determine which row the click occurred on.


protected override void Render(System.Web.UI.HtmlTextWriter writer)
    {
        AddRowSelectToGridView(gridView);
        base.Render(writer);
    }

    private void AddRowSelectToGridView(GridView gv)
    {
        try
        {
            foreach (GridViewRow row in gv.Rows)
            {
                row.Attributes["onmouseover"] = "this.style.cursor='hand';this.style.textDecoration='underline';";
                row.Attributes["onmouseout"] = "this.style.textDecoration='none';";
                row.Attributes.Add("onclick", Page.ClientScript.GetPostBackEventReference(gv, "Select$" + row.RowIndex.ToString(), true));
            }
        }
        catch (Exception ex)
        {
        }

    }

try this code,u can select the row..


Arguably the 'correct' event to use to detect when a DataGridView row is selected is SelectionChanged.

dataGridView1.SelectionChanged += new EventHandler(dataGridView1_SelectionChanged);

void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
    // code that happens after selection
}

The problem with this event is that the event signature only has a plain EventArgs with not special information about the DataGridView. Also this responds to any source of the selection changing, not only the row header being selected.

Depending on your exact needs the bemused's answer of RowHeaderMouseClick could well be better.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号