1

我一直坚持这一点,并尝试了一些不同的方法来使用更新按钮来更新我从网格视图中选择的记录。我在表 ID 和名称中有 2 列。我选择了记录,它会填充一个带有名称的文本框......这很好用。在使用更新按钮单击事件将其拉入文本框后,我只需要获取相同的记录并从相同的文本框中更新名称。我还有另外两个可以正常工作的按钮,它们是“添加”和“删除”,我也会添加该代码,但这里是代码:

这就是我在页面加载或调用方法时填充网格视图的方式:

private void PopulateCompanyListGrid() //This will populate the grid with the table data on page load
    {
        IList<Company> companies;

        using (var context = new IMSDBContext())
        {
            companies = context.Companies.ToList();
        }

        grdvwCompanyList.DataSource = companies;
        grdvwCompanyList.DataBind();
    }

这是设置网格视图的方式:

<asp:GridView runat="server" ID="grdvwCompanyList" OnSelectedIndexChanged="SelectGridRow" DataKeyNames="Id, Name" AllowSorting="True" AutoGenerateSelectButton="True"></asp:GridView>

这就是我将所选记录放入文本框中的方式:

public void SelectGridRow(object sender, EventArgs e) //This will populate the textbo with the row selected from the gridview
    {
        GridViewRow name = grdvwCompanyList.SelectedRow;

        if (name != null)
        {
            var dataKey = grdvwCompanyList.DataKeys[name.RowIndex];
            if (dataKey != null)
                txtCompanyName.Text = (string)dataKey["Name"];


        }
    }

这就是我添加记录的方式:

protected void btnAdd_Click(object sender, EventArgs e) // This method adds a record to the database
    {
        if (btnAdd.Text == "Add") // Clears the textbox and notification label and calls method to change name of button if the button says "Add"
        {
            txtCompanyName.Text = "";
            lblCompanyNameNotification.Text = "";
            ButtonChangeAddToSave();
        }
        else if (btnAdd.Text == "Save") // Checks if the button says "Save" and compares textbox and database for a matching record 
        {
            IMSDBContext context = new IMSDBContext();
            Company CompanyCheck = context.Companies.SingleOrDefault(Company => Company.Name == txtCompanyName.Text);

            if (CompanyCheck != null) // Displays a notification if there is already a matching record
            {
                lblCompanyNameNotification.Text = "There is already a Company with that name.";
            }
            else if(txtCompanyName.Text == null)
            { 
                lblCompanyNameNotification.Text = "Please enter a name of a company";
            }
            else if (txtCompanyName.Text != null) // Write the record to the database if no matching record in the database
            {
                Company n = new Company();
                n.Name = txtCompanyName.Text.ToString();


                context.Companies.Add(n);
                context.SaveChanges();

                txtCompanyName.Text = "";
                lblCompanyNameNotification.Text = "";

                ButtonChangeSaveToAdd();
            }
        }
        PopulateCompanyListGrid(); // Calls method to repopulate the gridview
    }
4

1 回答 1

1

在标记中添加一个隐藏字段来保存公司 ID:

<asp:HiddenField ID="hdnCompanyId" runat="server" ></asp:HiddenField>

在 selectGridRow 方法中,使用公司 ID 填充隐藏字段:

public void SelectGridRow(object sender, EventArgs e) //This will populate the textbo with the row selected from the gridview
{
    GridViewRow name = grdvwCompanyList.SelectedRow;

    if (name != null)
    {
        var dataKeys = grdvwCompanyList.DataKeys[name.RowIndex];
        if (dataKeys["Name"] != null)
            txtCompanyName.Text = (string)dataKeys["Name"];
        if (dataKeys["Id"] != null)
            hdnCompanyId.Value = dataKeys["Id"].ToString();
    }
}

在 btnUpdate_Click 方法中,通过 Id 获取公司并更新它:

protected void btnUpdate_Click(object sender, EventArgs e) 
{
    int companyId;
    string companyName = txtCompanyName.Text;
    if(int.TryParse(hdnCompanyId.Value, out companyId)){

        IMSDBContext context = new IMSDBContext();
        Company company = context.Companies.SingleOrDefault(Company => Company.Id == companyId);

        if (company != null && txtCompanyName.Text != "")
        {
            company.Name = companyName;
            context.SaveChanges();

        }
        else
        {
            lblCompanyNameNotification.Text = "The  Company does not exist.";
        }
    }

    PopulateCompanyListGrid(); // Calls method to repopulate the gridview            
}
于 2013-08-10T22:11:58.950 回答