Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从任何GridView行获取值(不仅是选定的)_C#_Asp.net_Gridview - Fatal编程技术网

C# 从任何GridView行获取值(不仅是选定的)

C# 从任何GridView行获取值(不仅是选定的),c#,asp.net,gridview,C#,Asp.net,Gridview,我正在尝试使用这个代码。但它不起作用。怎么了 问题图片: 受保护的无效按钮1\u单击(对象发送者,事件参数e) { 对于(int i=0;i

我正在尝试使用这个代码。但它不起作用。怎么了

问题图片:

受保护的无效按钮1\u单击(对象发送者,事件参数e)
{
对于(int i=0;i
使用以下行

 TextBox2.Text += GridView4.Rows[i].Cells[1].Value.ToString() + ", ";
  • 您必须
    在GridView中查找控件
    ,并强制转换它,例如
    标签
  • 将值添加到
    列表中
  • 通过逗号连接列表
在解决方案中:

List<string> valueArray = new List<string>();
for (int i = 0; i < GridView4.Rows.Count; i++)
{
    // Label1 is ID of your control in GridView
    string value = ((Label)GridView4.Rows[i].Cells[1].FindControl("Label1")).Text;
    valueArray.Add(value);
}
TextBox2.Text = string.Join<string>(",", valueArray); // seperate values by ,
List valueArray=new List();
对于(int i=0;i
什么在您的代码中不起作用?您是否收到异常错误?TextBox2.Text是否添加了任何值?需要更新您的问题,而不仅仅是“它不起作用,什么是错误的”,理想情况下是预期结果==foo和实际结果==bar,这是数据网格中的内容“没有错误。当我单击Buton1时,会在文本框“,”中显示输出,其中包含树线gridview。我添加了图片。
单元格中有控件[1] 
。尝试
查找控件(“MyLabel”)
并使用它,而不是
文本。
List<string> valueArray = new List<string>();
for (int i = 0; i < GridView4.Rows.Count; i++)
{
    // Label1 is ID of your control in GridView
    string value = ((Label)GridView4.Rows[i].Cells[1].FindControl("Label1")).Text;
    valueArray.Add(value);
}
TextBox2.Text = string.Join<string>(",", valueArray); // seperate values by ,
protected void Button_SonucKaydet_Click(object sender, EventArgs e)
{// I did with Asif.Ali and  Alex Kudryashev's opinion.
    try
    {    
         OleDbConnection con = new OleDbConnection(VTYolu);
         con.Open();
         foreach (GridViewRow row in GridView4.Rows)
         {
             Label innerLabel = (Label)row.FindControl("Label1x");
             DropDownList innerDropdown = (DropDownList)row.FindControl("DropDownList1x");
             TextBox innerTextBox = (TextBox)row.FindControl("TextBox4x");

             string Ekle1 = "UPDATE Dat_Odev SET OdevKontrolTarihi=@Tarih, OdevSonucu=@Sonuc, SonucAciklama=@Aciklama WHERE Kimlik=@id";
             OleDbCommand Komut1 = new OleDbCommand(Ekle1, con);
             Komut1.Parameters.AddWithValue("@Tarih", DateTime.Now.ToString());
             Komut1.Parameters.AddWithValue("@Sonuc", innerDropdown.SelectedValue);
             Komut1.Parameters.AddWithValue("@Aciklama", innerTextBox.Text);
             Komut1.Parameters.AddWithValue("@id", Int32.Parse(innerLabel.Text));
             Komut1.ExecuteNonQuery();
        }
        con.Close();
        GridView1.DataBind();
    }
    catch (Exception HataKodu)
    {//Hata oluştuğunda çalışacak kodlar
        Label_Hata.Visible = true;
        Label_Hata.Text = "Ödev sonuçlarını kaydederken bir hata oluştu. Veya Veritabanı hatası. Sistem yöneticisi ile irtibata geçiniz. (Hata kodu: " + HataKodu + ")";
    }
}