【问题标题】:getting the value from the gridview cell in dynamic fields using row updating event使用行更新事件从动态字段中的 gridview 单元格获取值
【发布时间】:2020-05-28 05:05:18
【问题描述】:

我正在尝试从 gridview 获取值,但我得到的是空字符串。

这是用户界面:

    <asp:GridView runat="server" ID="GroceryGrid"
    ItemType="DAL.Grocery" DataKeyNames="GroceryId"
    SelectMethod="GroceryGrid_GetData"
    AutoGenerateColumns="false" CssClass="table table-responsive" AutoGenerateDeleteButton="True"
    AutoGenerateEditButton="True" OnRowDeleting="GroceryGrid_RowDeleting" 
    OnRowEditing="GroceryGrid_RowEditing" OnRowUpdating="GroceryGrid_RowUpdating">
    <Columns>
        <asp:DynamicField DataField="GroceryName" HeaderText="GroceryName"/>
        <asp:DynamicField DataField="Cup" HeaderText="Cup" />
        <asp:DynamicField DataField="Spoon" HeaderText="Spoon" />
        <asp:DynamicField DataField="Gram" HeaderText="Gram"/>
        <asp:DynamicField DataField="Piece" HeaderText="Piece"/>
    </Columns>
    </asp:GridView>

及其正确获取数据

     protected void GroceryGrid_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        GridViewRow row = GroceryGrid.Rows[e.RowIndex];
        int groceryId = Convert.ToInt32(GroceryGrid.DataKeys[e.RowIndex].Values[0]);
        string imeNamirnice = row.Cells[0].Text;
        double salica = Convert.ToInt32((row.Cells[2].Text));
        double zlica = Convert.ToInt32((row.Cells[3].Text));
        double gram = Convert.ToInt32((row.Cells[4].Text));
        double komad = Convert.ToInt32((row.Cells[5].Text));
        using (RwaContext update = new RwaContext())

这是试图从单元格中获取值的代码部分,但我得到的是空值 比如

string imeNamirnice = row.Cells[0].Text;

它返回:"" - 空字符串

【问题讨论】:

    标签: entity-framework webforms


    【解决方案1】:

    这是一个例子,你很接近。展示了几种不同的方法来获取 DataKey 值,具体取决于您是更新还是编辑。

    前端

    <form id="form1" runat="server">
            <div>
    
                <asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound" DataKeyNames="id" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" AutoGenerateColumns="true">
                    <Columns>
                        <asp:CommandField ButtonType="Button" CausesValidation="false" ShowEditButton="true" EditText="Edit Me" UpdateText="Update Me"/>
                    </Columns>
                </asp:GridView>
    
            </div>
        </form>
    

    代码隐藏

        public partial class MyProduct
        {
            public string id { get; set; }
            public string name { get; set; }
            public string sku { get; set; }
        }
    
    public partial class _testForPw : BaseStorePage
    {
    
        protected void Page_Load(Object sender, EventArgs e)
        {
            Collection<MyProduct> products = new Collection<MyProduct>();
            products.Add(new MyProduct() { id = "123", name = "Test123", sku = "Sku1234" });
            GridView1.DataSource = products;
            GridView1.DataBind();
        }
    
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
    
        }
    
        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            string id = (string)GridView1.DataKeys[e.NewEditIndex].Value;
    
            if (!string.IsNullOrWhiteSpace(id))
            {
                // convert to integer and then execute code
            }
        }
    
        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            string id = (string)GridView1.DataKeys[e.RowIndex].Value;
    
            if (!string.IsNullOrWhiteSpace(id))
            {
                // convert to integer and then execute code
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2010-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-05
      相关资源
      最近更新 更多