0

I search on google but i can't find the solution.e.Rows or e.row not working in my visual studio 2010.How can i get this property.I want that when i click on "edit" button then gridview have different columsn and one column name is "Data Type" then this column fill with "dropdownlist" and i select value from drop down list.My dropdown control id is "dropdownlist1" .How can i do it?.Here is my code "gridview edit" event code:

protected void GridView2_RowEditing(object sender, GridViewEditEventArgs e)
{
  DataTable t = (DataTable)Session["MyDataTable"];
  GridView2.EditIndex = e.NewEditIndex;
  DropDownList ddlName =
                (DropDownList)e.Row.FindControl("dropdownlist1"); // HERE IS ERROR AT "Row" 
  ddlName.DataTextField = "Data Type";
  ddlName.DataValueField = "Data Type";
  GridView2.DataSource = t;
  GridView2.DataBind();
}

Here is my aspx code :

  <asp:GridView ID="GridView2" runat="server" AutoGenerateDeleteButton="True" 
        AutoGenerateEditButton="True" BackColor="#DEBA84" BorderColor="#DEBA84" 
        BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2" 
        ShowHeaderWhenEmpty="True" onrowcancelingedit="GridView2_RowCancelingEdit" 
        onrowdeleting="GridView2_RowDeleting" onrowediting="GridView2_RowEditing" 
        onrowupdating="GridView2_RowUpdating" AutoGenerateColumns="False" 
        Width="92px">
        <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
        <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
        <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
        <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
        <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
        <SortedAscendingCellStyle BackColor="#FFF1D4" />
        <SortedAscendingHeaderStyle BackColor="#B95C30" />
        <SortedDescendingCellStyle BackColor="#F1E5CE" />
        <SortedDescendingHeaderStyle BackColor="#93451F" />
    </asp:GridView>

</div>
<asp:TextBox ID="TextBox1" runat="server" Width="137px"></asp:TextBox>
<asp:DropDownList ID="DropDownList1" runat="server" Height="20px" 
    onselectedindexchanged="DropDownList1_SelectedIndexChanged" Width="140px">
    <asp:ListItem>int</asp:ListItem>
    <asp:ListItem>Varchar</asp:ListItem>
</asp:DropDownList>
<asp:CheckBox ID="Null" runat="server" />
<asp:CheckBox ID="Primary" runat="server" />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Insert" 
    style="margin-left: 59px" Width="56px" />
<asp:Button ID="Button2" runat="server" Height="33px" style="margin-left: 76px" 
    Text="Create Table" Width="94px" />

Is I'm on the right way?or how can i do it?And also tell me how can i update the gridview row.I insert values in gridview manually.There is no database connectivity etc.Thanks

1
  • on which Event of GridView You are trying e.Row?? Commented Apr 14, 2016 at 13:43

1 Answer 1

1

You can retrieve the row with the index:

GridViewRow row = GridView2.Rows[e.NewEditIndex];


UPDATE

You can put the DropDownList in a TemplateField of the GridView:

<asp:GridView ...>
    ...
    <asp:TemplateField>
        <ItemTemplate>
            <asp:Literal runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "DataTypeValue")) %>' />
        </ItemTemplate>
        <EditItemTemplate>
            <asp:DropDownList ID="ddlTypeName" runat="server" ... />
        </EditItemTemplate>
    </asp:TemplateField>
    ...
</asp:GridView>

(I showed an optional ItemTemplate in case you want to display the selected data type)

Then you can retrieve the DropDownList in your event handler:

GridViewRow row = GridView2.Rows[e.NewEditIndex];
DropDownList ddlTypeName = (DropDownList)row.FindControl("ddlTypeName");
...
Sign up to request clarification or add additional context in comments.

8 Comments

But i want that when i click on "edit" button then gridview have column which have name "Data Type" then this column fill with "dropdownlist" and i select value from drop down list.my dropdown control id is "dropdownlist1" .How can i do it?
You can show your markup and add that new question to the one that you already asked. If the DropDownList is in a TemplateField of the GridView, you should be able to retrieve it with FindControl, like you do in your code sample (after replacing ddlName with dropdownlist1, if that is the name of your control).
I don't want to add new column or template field,I want that my "Data Type" column fill with "dropdownlist" when i press edit.
Then, your "Data type" column should be a TemplateField. I don't see a better way to do it.
But i don't want to add extra column,if i add template field then extra column will be added,
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.