Link to home
Start Free TrialLog in
Avatar of navinbabu
navinbabuFlag for India

asked on

Edit and Row command in a grid view

Hello,

I'm having a row command and edit in a grid. When I click the edit it triggers the same action as the rowcommand


 Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand


What should I do about the edit command ?
Avatar of intlaqa
intlaqa
Flag of Egypt image

Hi,

What are you doing in the Row Command? anyway start the code in your RowCommand with something like:

if(e.CommandName = "MyCustomCommand")
{
// your code here
}

And if you have custom buttons in your rows, just give them a CommandName, for example CommandName="MyCustomCommand".

If you do not get it, please show me both your ASPX Grid View code and your RowCommand Codebehind code.
Avatar of navinbabu

ASKER

This is my Grid

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
                    BackColor="White" BorderColor="#CC9966" BorderStyle="None" BordrWidth="1px"
                    CellPadding="4" DataSourceID="SqlDataSource3" AllowPaging="True"
                       DataKeyNames="DeptId" OnItemDataBound="Item_Bound"
                       EnableModelValidation="True">
                    <RowStyle BackColor="White" ForeColor="#330099" />
                    <Columns>
                        <asp:BoundField DataField="DeptId"  HeaderText="DeptId" Visible="true" />
                             <asp:BoundField DataField="DeptName" HeaderText="Name"
                            SortExpression="DeptName" />
                        <asp:BoundField DataField="Course" HeaderText="Course"
                            SortExpression="Course" />
                        <asp:BoundField DataField="GPA" HeaderText="GPA"
                            SortExpression="GPA" />
                        <asp:BoundField DataField="GRE_V" HeaderText="GRE Verbal"
                            SortExpression="GRE_V" />
                        <asp:BoundField DataField="GRE_Q" HeaderText="GRE Quant"
                            SortExpression="GRE_Q" />
                        <asp:BoundField DataField="GRE_AWA" HeaderText="GRE AWA"
                            SortExpression="GRE_AWA" />
                        <asp:BoundField DataField="T_R" HeaderText="TOEFL Reading"
                            SortExpression="T_R" />
                        <asp:BoundField DataField="T_W" HeaderText="TOEFL Writting"
                            SortExpression="T_W" />
                        <asp:BoundField DataField="T_L" HeaderText="TOEFL Listening"
                            SortExpression="T_L" />
                        <asp:BoundField DataField="T_S" HeaderText="TOEFL Speaking"
                            SortExpression="T_S" />
                        <asp:BoundField DataField="IELTS" HeaderText="IELTS"
                            SortExpression="IELTS" />
                        <asp:BoundField DataField="GMAT" HeaderText="GMAT" SortExpression="GMAT" />
                        <asp:CommandField ShowEditButton="True" />
                        <asp:ButtonField  Text="Allot to  Student"  />
                    </Columns>
                    <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
                    <PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
                    <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
                    <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
                </asp:GridView>


Here is the backend Code :


    Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
        Dim index As Integer = Convert.ToInt32(e.CommandArgument)
        Dim row As GridViewRow = GridView1.Rows(index)
        Response.Redirect("Assign.aspx?deptid=" + row.Cells(0).Text)
    End Sub
Here is the DataSource

<asp:SqlDataSource ID="SqlDataSource3" runat="server"
                       ConnectionString="<%$ ConnectionStrings:db %>"
                       SelectCommand="SELECT * FROM [Departments] WHERE (([UnivId] = @UnivId) AND ([DeptName] = @DeptName) AND ([Course] = @Course))">
                       <SelectParameters>
                           <asp:QueryStringParameter Name="UnivId" QueryStringField="id" />
                           <asp:ControlParameter ControlID="ddl_dept" Name="DeptName"
                               PropertyName="SelectedValue" Type="String" />
                           <asp:ControlParameter ControlID="ddl_course" Name="Course"
                               PropertyName="SelectedValue" Type="String" />
                       </SelectParameters>
                   </asp:SqlDataSource>

and Also can I avoid using the DeptId which I'm including as a column . Which I'm calling in the row command .

1) I tried assigning the DeptId as datakey of the grid . And tried calling it back in the server side by

GridView1.SelectedValue

I dont get any value.

2) Here I added a edit command button and it also triggers the same action as the my custom command. How can I correct this ?
ASKER CERTIFIED SOLUTION
Avatar of intlaqa
intlaqa
Flag of Egypt image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks intlaga it worked like a charm.

I recently converted a asp.net website to web application. Now every change I make in the Visual studio 2010 are not visible in the running local host .

For example.

The above problem page name is University.aspx and server side code University.aspx.vb

I right click view in browser for the University.aspx which triggers a local host and open a page in browser etc.

Now when I used asp.net website I used to make the change in the visual studio and just refresh the page in my browser to see the new changes but after converting to the web application to see the changes I made in the VS. I need to again right click the file for View in browser. Which is very idiotic.

How does these things work usually ? How do people observe changes in industry ?
 
Hi navinbabu, you're welcome.

Yes, working in a Web Application project is different, you have to Rebuild the project.. etc after making the changes in classes.. etc.

While the page in the browser (After clicking on View in Browser) you can play with the ASPX by changing styles, adding <div>'s .. etc and just save the page and refresh the browser to see the changes.

But when you change the code-behind or some classes, add reference to something.. etc, you have to build it first, I usually hit CTRL+SHIFT+B to build the project and go to the browser and refresh to see the changes.
Any work around ? My habit of checking everything in the browser make this quite difficult and I have a cloud service attached to the solution so building the app takes a lot of time.
And again can I convert the application back to the website ?
Hi,

I am stuck with some old projects and cannot take the risk to move to VS 2010 soon. I am sorry I can not help in this.

Converting Web Application project back to Website is not easy in current versions of VS. But perhaps VS 2010 has something new features that makes this kind of convertion easy.
ok . But One thing I find very useful in vs 2010 is auto complete for the table name and even their columns which is quite useful