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 ?
LVL 2
navinbabuAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

intlaqaCommented:
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.
0
navinbabuAuthor Commented:
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
0
navinbabuAuthor Commented:
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 ?
0
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

intlaqaCommented:
Hi,

This is your custom command, right?

<asp:ButtonField  Text="Allot to  Student"  />

If yes, please add the CommandName property, to be like this:

<asp:ButtonField CommandName="Allot"  Text="Allot to  Student"  />

Then, make your  GridView1_RowCommand like this:

    Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
        If e.CommandName = "Allot" Then
            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 If
    End Sub

I found that you are just redirecting the user to another page (assign.aspx) , you can use a HyperLinkField instead of the custom ButtonField, so you may remove:

<asp:ButtonField  Text="Allot to  Student"  />

And replace it with:

<asp:HyperLinkField Text="Allot to  Student" DataNavigateUrlFields="Deptid" DataNavigateUrlFormatString="Assign.aspx?deptid={0}" />
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
navinbabuAuthor Commented:
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 ?
 
0
intlaqaCommented:
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.
0
navinbabuAuthor Commented:
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.
0
navinbabuAuthor Commented:
And again can I convert the application back to the website ?
0
intlaqaCommented:
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.
0
navinbabuAuthor Commented:
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
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.