如何在Gridview中获取特定的调用值

时间:2019-02-21 19:57:54

标签: c# asp.net

我实际上是在使用GridView表来显示一些产品属性,例如名称,价格和数量。然后,在调用Datashow_RowUpdating函数时,我减去了“数量”单元格的值。现在,我想获得同一产品的名称,并将其保存在列表中并打印出来,但是我无法获得该产品的名称。我尝试了类似Datashow.Rows[e.RowIndex].Cells[2].Text.ToString();
的其他方法 但没有运气。我附上了包含更新代码的更新功能,请帮助!!!! 谢谢

protected void Datashow_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    try
    {
        using (SqlConnection sqlcon = new SqlConnection(connectionstring))
        {
            //ObjectQuery<Med> filteredproduct = new ObjectQuery<Med>("Select Value p From Med AS p ",Med);
            //LstProductsChosen.DataSource = products;
            //LstProductsChosen.DataValueField = "ID";
            //LstProductsChosen.DataTextField = "Name";


            sqlcon.Open();
            string query = "UPDATE Med SET Quantity = Quantity - @Quantity WHERE ID=@id";
            //Med q = new Med { Name = "eee" , Company = "new" };
            SqlCommand cmd = new SqlCommand(query, sqlcon);
            //cmd.Parameters.AddWithValue("@Quantity", (Datashow.Rows[e.RowIndex].FindControl("TextQuantity") as TextBox).Text.Trim());

            cmd.Parameters.AddWithValue("@Quantity", (Datashow.Rows[e.RowIndex].FindControl("TextQuantity") as System.Web.UI.WebControls.TextBox).Text.Trim());
            cmd.Parameters.AddWithValue("id", Convert.ToInt32(Datashow.DataKeys[e.RowIndex].Value.ToString()));
            cmd.ExecuteNonQuery();
            Datashow.EditIndex = -1;
            populategridview();
            Addinlist();
            GridViewRow row = Datashow.Rows[e.RowIndex];

            string c = Datashow.Rows[e.RowIndex].Cells[2].Text.ToString();
            Console.WriteLine(c);
            Med q = new Med()
            {
                Name = c ,
                Price = 100,
                Company = "Reck",
                Discount = 10,
                Expiry = 2018,
                Potency = "30",
                Quantity = 10,
                Size = "10ml"
            };
            meds.Add(q);
            LstProductsChosen.Items.Add("q" + q.Name);

            //LblSucessMsg.Text = "selected " + row.Cells[2].Text;
        }
    }
    catch (Exception ex)
    {
        LblSucessMsg.Text = "";
        LblErrorMsg.Text = ex.Message;
    }
}

1 个答案:

答案 0 :(得分:0)

更简单的方法是获取当前选定的行:

import socket    
class routers():
def __init__(self, router_name, user, password):
    try:
        self.user=user
        self.password=password
        self.router_name=router_name
        self.ip=socket.gethostbyname(router_name)#####
        self.remote = paramiko.SSHClient()            
 self.remote.set_missing_host_key_policy(paramiko.AutoAddPolicy())

    except socket.gaierror:
        print("Device name %s unresolved" %router_name)
        self.user = "Null"
        self.password = "Null"
        self.ip = "0.0.0.0"
        self.remote = paramiko.SSHClient()            
 self.remote.set_missing_host_key_policy(paramiko.AutoAddPolicy())

注意:单元格基于0索引。第一个单元格的索引为0,第二个单元格的索引为1,依此类推。

您也可以尝试以下操作:

GridViewRow row = DataShow.SelectedRow;

// And you respective cell's value
string textValue = row.Cells[2].Text;