简体   繁体   中英

how can i get values of gridview that i created in code behind when i click button

I have a problem about getting values from gridview that i created in code behind. I created dynamic gridview. I filled DataSource from Database. These are my codes:

 SqlCommand cmd = new SqlCommand("xxx", noz);
            cmd.CommandType = CommandType.StoredProcedure;
            noz.Open();
            i = 1;
            SqlDataReader rd = cmd.ExecuteReader();
            if (rd.HasRows)
            {
                while (rd.Read())
                {
                   string isim = "name" + i;
                    Label name4 = new Label();
                    name4.ID = isim;                  
                    name4.Text = rd3[1].ToString() + " için değerlendirme yapınız";
                    name4.Font.Bold = true;                    
                    ph.Controls.Add(name4);
                    noz2.Open();
                    SqlDataAdapter sda = new SqlDataAdapter("xxx", noz2);
                    sda.SelectCommand.CommandType = CommandType.StoredProcedure;
                    GridView gv = new GridView();
                    gv.ID = "_gridview" + i;
                    Queue q = new Queue();
                    q.Enqueue(i);
                    DataTable dt = new DataTable();
                    sda.Fill(dt);
                    dt.Columns.Add("Puanlar", typeof(string));
                    gv.DataSource = dt;
                    gv.DataBind();
                    ph.Controls.Add(gv); //ph is my placeholder//
                    gv.Height = 500;
                    gv.Width = 980;
                    i++;
                    noz2.Close();
                    foreach (GridViewRow r in gv.Rows)
                    {
                        TextBox ddl3Selection = new TextBox();
                        ddl3Selection.Width = 800;

                        r.Cells[2].Controls.Add(ddl3Selection);
                    }
                }
                noz.Close();

I have no problem so far. But when i want to use click button, i can't get gridviews's rows. I get rowcount 0.

for (int a = 1; a < i; a++)
            {
                GridView gv = new GridView();
                gv.ID = "_gridview" + a;
                Label lbl = new Label();
                lbl.ID = "name" + a;

                for (int n = 0; n < gv.Rows.Count; n++)
                {                   
                    SqlCommand cmd = new SqlCommand("xxx", noz);
                    cmd.CommandType = CommandType.StoredProcedure;
                    GridViewRow row = gv.Rows[n];
                    int soruid =Convert.ToInt16(row.Cells[0].Text);
                    string cevap = row.Cells[2].Text;
                    string firmaadi = lbl.Text;

                    cmd.Parameters.AddWithValue("@Firma_Ad", firmaadi);
                    cmd.Parameters.AddWithValue("@Soru_Id", soruid);
                    cmd.Parameters.AddWithValue("@Soru_Cevap", cevap);
                    cmd.Parameters.AddWithValue("@Cevaplayan", "");
                    noz.Open();
                    int rd2 = cmd.ExecuteNonQuery();
                    noz.Close();
                }

How can i get values from gridview?

there is two different grid. First grid have data source and the second is nothing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM