Question : Datagridview row updating event

Hi,

I have a dropdownlist in the gridview.
When i edit and click the update button it does not seem to make any difference.
The row stays in the edit mode.
can you see and let me kow what is wrong  with the code?

Thanks




Private Function getClassDD() As DataSet
        Dim conn As New SqlConnection(myconn)
        conn.Open()
        Dim strquery As String = Nothing
        strquery = "Select distinct(Class) from Production.Product"
        Dim mycommand As New SqlCommand(strquery, conn)
        Dim myadapter As New SqlDataAdapter(mycommand)
        Dim ds As New DataSet
        myadapter.Fill(ds)
        conn.Close()
        Return ds
    End Function

    Protected Sub GridView1_RowCancelingEdit(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCancelEditEventArgs) Handles GridView1.RowCancelingEdit
        GridView1.EditIndex = -1
        GridView1.DataBind()
    End Sub

    Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow And (e.Row.RowState And DataControlRowState.Edit) > 0 Then
            Dim dd_MyClass As DropDownList = DirectCast(e.Row.FindControl("dd_Class"), DropDownList)
            dd_MyClass.DataSource = getClassDD()
            dd_MyClass.DataTextField = "Class"
            dd_MyClass.DataValueField = "Class"
            dd_MyClass.DataBind()
        End If
    End Sub
    Protected Sub GridView1_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles GridView1.RowEditing
        GridView1.EditIndex = e.NewEditIndex
        GridView1.DataBind()
    End Sub
    Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView1.RowUpdating
        Dim dd_MyClass As DropDownList = DirectCast(GridView1.Rows(e.RowIndex).FindControl("dd_Class"), DropDownList)
        Dim val As String = dd_MyClass.SelectedIndex
        e.NewValues.Add("Class", val)
        GridView1.DataBind()
    End Sub

Answer : Datagridview row updating event

If you want to enter data from keyboard - you need a while loop, something like in the snippet.

Can't get why do you need sorting?
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
    class Program
    {
        static void Main(string[] args)
        {
            int[] salaryRange = { 300, 400, 500, 600, 700, 800, 900, 1000};
            int[] frequency = { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
            bool doContinue = true;
            while(doContinue)
            {
                Console.WriteLine("Please enter salary");
                string resultStr = Console.ReadLine();
                int result;
                bool canParse = int.TryParse(resultStr, out result);
                if (canParse)
                {
                    if (result < 200)
                    {
                        Console.WriteLine("Wrong input - salary cannot be less than 200");
                        continue; //ask for input again
                    }
                    else
                    {
                        for(int i =0;i<frequency.Length;i++){
                            if (i==8)
                            {
                                frequency[i] = frequency[i] + 1;
                            }
                            else if (result < salaryRange[i])
                            {
                                frequency[i] = frequency[i] + 1;
                                break;
                            }
                        }
                    }

                }
                else
                {
                    Console.WriteLine("Wrong input - should be numeric");
                    continue; //ask for input again
                }
                Console.WriteLine("Do you want to continue? Y/N");
                resultStr = Console.ReadLine();
                if (!(resultStr.ToLower() == "y"))
                {
                    doContinue = false;
                }

            }// end while

            // print results
            Console.WriteLine("\n\n===============\nFrequency");
            for (int i = 0; i< frequency.Length ; i++)
            {
                string lower;
                string upper;
                if (i == 0)
                {
                    lower = "200";
                }
                else
                {
                    lower = "" + (salaryRange[i-1]);
                }
                if (i == 8)
                {
                    upper = "over";
                }
                else
                {
                    upper = "" + (salaryRange[i]-1);
                }
                Console.WriteLine(lower + " - " + upper + ": \t" + frequency[i]);
            }
           
            Console.ReadLine();

        }
    }
Random Solutions  
 
programming4us programming4us