Hunter Mitchell
Hunter Mitchell

Reputation: 7293

Selecting row of DataGridView?

How do i have the whole row of a data grid view. I have no idea where to start. I have tried this, but has NO idea where to PUT it. :

dataGrid.Rows[index].Selected = true;

I am just looking for a peice of code that will help me! Thanks

Upvotes: 0

Views: 128

Answers (1)

Jupaol
Jupaol

Reputation: 21365

It seems you want to do something like this:

protected void Page_Load(object sender, EventArgs e)
{
    this.FillGrid();
}
protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
    this.GridView1.SelectedIndex = e.NewSelectedIndex;
    this.FillGrid();
}

private void FillGrid()
{
    this.GridView1.DataSource = new[] { "Hello", "World" };
    this.GridView1.DataBind();
}

And in the aspx file:

<asp:GridView ID="GridView1" runat="server" 
        onselectedindexchanging="GridView1_SelectedIndexChanging">
        <Columns>
            <asp:CommandField ShowSelectButton="True" />
        </Columns>
        <SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
    </asp:GridView>

Which produces this output:

enter image description here

Upvotes: 1

Related Questions