none
How to assign GridView values to different controls RRS feed

  • Question

  • Hi,

    Greetings!

    I have a GridView control. I am assigning a DataSource to the grid at runtime.

    Now, I have added a select button and want to get the values of that GridView into multiple TextBox controls.

    But at runtime I am getting empty values:

    <asp:GridView ID="gvMyTable" runat="server" OnSelectedIndexChanging = "gvMyTable_RowSelect1" 
    	OnSelectedIndexChanged = "gvMyTable_RowSelect2">
        <Columns>
            <asp:CommandField ShowSelectButton="True">
                <ControlStyle CssClass="grvSelectButton" />
            </asp:CommandField>
        </Columns>
    </asp:GridView>


    	OnSelectedIndexChanged = "gvMyTable_RowSelect2">
        <Columns>
            <asp:CommandField ShowSelectButton="True">
                <ControlStyle CssClass="grvSelectButton" />
            </asp:CommandField>
        </Columns>
    </asp:GridView>
    DataTable dtMyTable = new DataTable();
    dtMyTable.Columns.Add("col1", typeof(string));
    dtMyTable.Columns.Add("Col2", typeof(string));
    dtMyTable.Columns.Add("Col3", typeof(string));
    protected void gvMyTable_RowSelect1(object sender, GridViewSelectEventArgs e)
    {
        GridView gv1 = (GridView)sender;
        GridViewRow gvr1 = (GridViewRow)gv1.Rows[e.NewSelectedIndex];
        var RowCellVal = gvr1.Cells[1].Text;
        RowCellVal = gvr1.Cells[2].Text;
    }
    protected void gvMyTable_RowSelect2(Object sender, EventArgs e)
    {
        GridViewRow gvr1 = gvMyTable.SelectedRow;
        var RowCellVal = gvr1.Cells[1].Text;
        RowCellVal = gvr1.Cells[2].Text;
    
    }

    What am I doing wrong?

    Your help is highly appreciated!

    Regards,

    Abhinay


    • Edited by abhinaybuiss Monday, April 1, 2019 1:44 PM Code correction
    • Moved by CoolDadTx Monday, April 1, 2019 2:31 PM ASP.NET related
    Monday, April 1, 2019 1:41 PM

All replies