Retrieve values from GridView by changing a value

2019-07-09 22:04发布

问题:

I'm having problems retrieving the current row by changing the status of a DropDownList in the row. My code for the GridView is:

<asp:GridView ID="grdMappingList" runat="server" OnPageIndexChanging="grdMappingList_PageIndexChanging"  AutoGenerateColumns="false" AllowPaging="true" PageSize="10"  ShowHeaderWhenEmpty="true" UseAccessibleHeader="true" CssClass="table table-hover table-striped segment_list_tbl" >
<PagerStyle CssClass="grid_pagination" HorizontalAlign="Right" VerticalAlign="Middle" BackColor="#DFDFDF" />
<Columns>
    <asp:BoundField HeaderText="Mapping Id" DataField="MAPPING_ID"></asp:BoundField>
    <asp:BoundField HeaderText="PDM Name" DataField="PDM_NAME"></asp:BoundField>   
    <asp:BoundField HeaderText="PDM EntityID" DataField="PDM_ENTITY_ID" Visible="false"></asp:BoundField>   
    <asp:TemplateField HeaderText="Mapping Status" HeaderStyle-CssClass="width_120 aligned_center" ItemStyle-CssClass="width_120 aligned_center" Visible="true">
            <ItemTemplate>
                <asp:DropDownList id="ddlMappingStatus" runat="server"  SelectedValue='<%# Bind("MappingCompleted") %>' OnSelectedIndexChanged="ddlMappingStatus_SelectedIndexChanged" AutoPostBack="true">
                    <asp:ListItem Text="Done"  Value="DONE"></asp:ListItem>
                    <asp:ListItem Text="Not Done"  Value="NOT DONE"></asp:ListItem>
                </asp:DropDownList>
            </ItemTemplate>
        </asp:TemplateField>    
    </Columns>
 </asp:GridView>

And the code behind is:

protected void ddlMappingStatus_SelectedIndexChanged(object sender, EventArgs e)
{
    int MappingID = Convert.ToInt16(grdMappingList.SelectedRow.Cells[0].Text);
    DropDownList ddgvOpp = (DropDownList)grdMappingList.SelectedRow.FindControl("ddlMappingStatus");
    if (ddgvOpp.Equals("DONE"))
    { 

    }
}

I am getting an error on this line:

DropDownList ddgvOpp  (DropDownList)grdMappingList.SelectedRow.FindControl("ddlMappingStatus");

and this line:

int MappingID = Convert.ToInt16(grdMappingList.SelectedRow.Cells[0].Text);

It seems that I can't retrieve the values! I dont know why. When I choose a new status from the DropDownList, I want to get all the values of the row in order to update the record.

回答1:

Unfortunately, SelectedRow does not mean what you think it means. It doesn't just simply mean the current row you are working on or the current row with the controls that you are using. That property must get set somehow. This is usually done through a "Select" button on the GridView, such as an <asp:ButtonField> with the command name "Select".

You really do not need this however. You just need the row you are working with. Since you are using the SelectedIndexChanged event of your DropDownList, you already have what you need. You just need to go about it a different way.

First, get the DropDownList. Then get the row of the DropDownList. Now do whatever you need with the row.

But, from your code, it looks like you may not even need the row. You just need the value of the DropDownList. So you could skip that all together. But if you do need the row, here is how you would do that too.

protected void ddlMappingStatus_SelectedIndexChanged(object sender, EventArgs e)
{
    DropDownList ddlMappingStatus = (DropDownList)sender;
    if(ddlMappingStatus.SelectedItem.Text.ToUpper() == "DONE")
    {
    }

    GridViewRow row = (GridViewRow)ddlMappingStatus.NamingContainer;
}

Now when you use <asp:BoundField> controls in your GridView, getting their values isn't very clean. You are forced to hardcode the column index like you were doing:

GridViewRow row = (GridViewRow)ddlMappingStatus.NamingContainer;
String mappingID = row.Cells[0].Text;

I tend to prefer using <asp:TemplateField> controls so I can use FindControl() to get what I am after. This prevents any reordering of the columns from breaking the code, as you'd have to hardcode different indexes. So for example, to find that DropDownList (since it already is a <asp:TemplateField>), you'd do something like this:

DropDownList ddlMappingStatus = (DropDownList)row.FindControl("ddlMappingStatus");