使用boundfield更新gridview [英] Update gridview using boundfield

查看:58
本文介绍了使用boundfield更新gridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,



我希望你们都做得很好。

我有一个关于使用BoundField更新GridView的问题。

我目前正在做的是我在GridView中添加了一个编辑按钮,一旦我点击它就会给我编辑字段并且更新取消按钮。

取消编辑按钮工作正常,但我想更新工作也好。



以下是我的HTML代码:



< ; asp:GridView ID =gvSubmittedCasesrunat =serverOnRowEditing =gvSubmittedCases_RowEditingOnRowUpdating =gvSubmittedCases_RowUpdating
OnRowCancelingEdit =gvSubmittedCases_RowCancelingEditCellPadding =4GridLines =NoneWidth =100%AutoGenerateColumns =FalseForeColor =#333333>
< AlternatingRowStyle BackColor =WhiteForeColor =#284775/>
< Columns>
< asp:BoundField DataField =SubmittedDateReadOnly =trueHeaderText =SubmittedDateItemStyle-Width =100SortExpression =SubmittedBy>
< / asp:BoundField>
< asp:BoundField DataField =SubmittedByVisible =trueReadOnly =trueHeaderText =Submitted ByItemStyle-Width =150SortExpression =Transaction Amount>
< / asp:BoundField>
<% - < asp:BoundField DataField =Display_NameVisible =trueHeaderText =Display NameItemStyle-Width =150SortExpression =Display Name>
< / asp:BoundField> - %>
< asp:BoundField DataField =CardNoHeaderText =卡号ItemStyle-Width =100SortExpression =CardNo>
< / asp:BoundField>
< asp:BoundField DataField =CardTypeReadOnly =trueHeaderText =Card TypeItemStyle-Width =100SortExpression =CardType>
< / asp:BoundField>
< asp:BoundField DataField =FraudTypeHeaderText =欺诈类型ItemStyle-Width =150SortExpression =FraudType>
< / asp:BoundField>
< asp:BoundField DataField =CaseIDVisible =trueReadOnly =trueHeaderText =CaseIDItemStyle-Width =150SortExpression =CaseID>
< / asp:BoundField>
< asp:BoundField DataField =RegionVisible =trueHeaderText =RegionItemStyle-Width =150SortExpression =Region>
< / asp:BoundField>
< asp:BoundField DataField =AuthorisationCodeVisible =trueReadOnly =trueHeaderText =Auth CodeItemStyle-Width =150SortExpression =Auth Code>
< / asp:BoundField>
< asp:BoundField DataField =TransactionDateVisible =trueHeaderText =Transaction DateItemStyle-Width =150SortExpression =TransactionDate>
< / asp:BoundField>
< asp:BoundField DataField =TransactionAmountVisible =TRUEHeaderText =Transaction AmountItemStyle-Width =150SortExpression =Transaction Amount>
< / asp:BoundField>
< asp:TemplateField>
< ItemTemplate>
< asp:Button ID =btnEditrunat =serverText =EditCommandName =Edit/>
< / ItemTemplate>
< EditItemTemplate>
< asp:Button ID =btnUpdaterunat =serverText =UpdateCommandName =Update/>
< asp:Button ID =btnCancelrunat =serverText =CancelCommandName =Cancel/>
< / EditItemTemplate>
< / asp:TemplateField>

< / Columns>







这是Code Behind。





 private void CaseView()
{
BusinessLayer k = new BusinessLayer( );
DataTable dt = k.ViewCases(UserID,UserType);
if(dt.Rows.Count> = 0)
{
gvSubmittedCases.DataSource = dt;
gvSubmittedCases.DataBind();
}
else
lblCases.Text =没有找到案例;
}

protected void gvSubmittedCases_RowEditing(object sender,GridViewEditEventArgs e)
{
gvSubmittedCases.EditIndex = e.NewEditIndex;
CaseView();
}





 protected void gvSubmittedCases_RowCancelingEdit(object sender,GridViewCancelEditEventArgs e)
{
gvSubmittedCases.EditIndex = -1;
CaseView();

}







此外,还有一个字段 FraudType 我想在用户点击编辑时丢弃。



我希望我已经清除了我的问题。

您可以询问是否需要清除任何东西。



注意:



我正在使用存储过程更新数据库,在SQL中





谢谢



我尝试了什么:



我搜索了网页,但是没有适当的解决方案对于BoundField +存储过程

解决方案

<$ h $ => https://msdn.microsoft.com/en-us/ library / system.web.ui.webcontrols.gridviewupdateeventargs(v = vs.110).aspx> a GridViewUpdateEventArgs instance [ ^ ]。



这包含三个可用于读取值的词典:

  • 密钥 [ ^ ]包含行的键值,由网格上的 DataKeyNames 属性指定。
  • NewValues [ ^ ]包含从行提交的新值。
  • OldValues [ ^ ]包含行中的原始值,以防您需要检查哪些字段已更改。


因此,要提取可能已更改的值:

  protected   void  gvSubmittedCases_RowUpdating( object  sender,GridViewUpdateEventArgs e)
{
string cardNo = Convert.ToString(e.NewValues [ CardNo]);
string fraudType = Convert.ToString(e.NewValues [ FraudType]);
string region = Convert.ToString(e.NewValues [ 区域]);
DateTime transactionDate = Convert.ToDateTime(e.NewValues [ TransactionDate]);
decimal transactionAmount = Convert.ToDecimal(e.NewValues [ TransactionAmount]);
...

gvSubmittedCases.EditIndex = -1;
CaseView();
}



您需要将网格的 DataKeyNames 设置为主键的名称,并从 e.Keys 字典中提取。



然后你应该可以调用你的存储过程来更新记录。



要将欺诈类型列更改为下拉列表,您需要使用 TemplateField

演练:显示下拉列表在GridView Web服务器控件中进行编辑 [ ^ ]


Hello,

I hope you all are doing great.
I have a question regarding Update GridView using BoundField.
What i am doing currently is i have added a edit button in the GridView and once i click on it it gives me editable fields and update and cancel button with it.
Cancel and Edit buttons are working fine but i want update to work as well.

Below is my HTML Code:

<asp:GridView ID="gvSubmittedCases" runat="server" OnRowEditing="gvSubmittedCases_RowEditing" OnRowUpdating="gvSubmittedCases_RowUpdating"  
                    OnRowCancelingEdit="gvSubmittedCases_RowCancelingEdit"   CellPadding="4" GridLines="None" Width="100%" AutoGenerateColumns="False"  ForeColor="#333333" >  
                        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                        <Columns>
                            <asp:BoundField DataField="SubmittedDate" ReadOnly="true" HeaderText="SubmittedDate" ItemStyle-Width="100" SortExpression="SubmittedBy" >
                            </asp:BoundField>
                            <asp:BoundField DataField="SubmittedBy" Visible="true" ReadOnly="true" HeaderText="Submitted By" ItemStyle-Width="150" SortExpression="Transaction Amount" >
                            </asp:BoundField>
                            <%--<asp:BoundField DataField="Display_Name" Visible="true" HeaderText="Display Name" ItemStyle-Width="150" SortExpression="Display Name" >
                            </asp:BoundField>--%>
                            <asp:BoundField DataField="CardNo" HeaderText="Card Number" ItemStyle-Width="100" SortExpression="CardNo" >
                            </asp:BoundField>
                            <asp:BoundField DataField="CardType" ReadOnly="true" HeaderText="Card Type" ItemStyle-Width="100" SortExpression="CardType" >
                            </asp:BoundField>
                            <asp:BoundField DataField="FraudType" HeaderText="Fraud Type" ItemStyle-Width="150" SortExpression="FraudType" >
                            </asp:BoundField>
                            <asp:BoundField DataField="CaseID" Visible="true" ReadOnly="true" HeaderText="CaseID" ItemStyle-Width="150" SortExpression="CaseID" >
                            </asp:BoundField>
                            <asp:BoundField DataField="Region"  Visible="true" HeaderText="Region" ItemStyle-Width="150" SortExpression="Region" >
                            </asp:BoundField>
                            <asp:BoundField DataField="AuthorisationCode"  Visible="true" ReadOnly="true" HeaderText="Auth Code" ItemStyle-Width="150" SortExpression="Auth Code" >
                            </asp:BoundField>
                            <asp:BoundField DataField="TransactionDate" Visible="true" HeaderText="Transaction Date" ItemStyle-Width="150" SortExpression="TransactionDate" >
                            </asp:BoundField>
                            <asp:BoundField DataField="TransactionAmount" Visible="TRUE" HeaderText="Transaction Amount" ItemStyle-Width="150" SortExpression="Transaction Amount" >
                            </asp:BoundField>
                            <asp:TemplateField>
                                <ItemTemplate>
                                    <asp:Button ID="btnEdit" runat="server" Text="Edit" CommandName="Edit" />
                                </ItemTemplate>
                                <EditItemTemplate>
                                    <asp:Button ID="btnUpdate" runat="server" Text="Update" CommandName="Update" />
                                    <asp:Button ID="btnCancel" runat="server" Text="Cancel" CommandName="Cancel" />
                                    </EditItemTemplate>
                                </asp:TemplateField>
                           
                            </Columns>




And this is Code Behind.


private void CaseView()
  {
      BusinessLayer k = new BusinessLayer();
      DataTable dt = k.ViewCases(UserID,UserType);
      if (dt.Rows.Count >= 0)
      {
          gvSubmittedCases.DataSource = dt;
          gvSubmittedCases.DataBind();
      }
      else
          lblCases.Text = "No Cases Found";
  }

  protected void gvSubmittedCases_RowEditing(object sender, GridViewEditEventArgs e)
  {
      gvSubmittedCases.EditIndex = e.NewEditIndex;
      CaseView();
  }



protected void gvSubmittedCases_RowCancelingEdit(object sender,GridViewCancelEditEventArgs e)
   {
       gvSubmittedCases.EditIndex = -1;
       CaseView();

   }




Also, there is one field FraudType that i want to be dropDown when the user clicks on edit.

I hope i have cleared my question.
You can ask if anything needs to get cleared.

Note:

I am using Stored Procedures to Update the database, which is in SQL


Thanks

What I have tried:

I have searched web, but there is no proper solution for BoundField + Stored Procedures

解决方案

The RowUpdating event passes in a GridViewUpdateEventArgs instance[^].

This contains three dictionaries you can use to read the values:
  • Keys[^] contains the key values for the row, as specified by the DataKeyNames property on the grid.
  • NewValues[^] contains the new values submitted from the row.
  • OldValues[^] contains the original values from the row, in case you need to check which fields have changed.

So, to extract the values which might have changed:

protected void gvSubmittedCases_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    string cardNo = Convert.ToString(e.NewValues["CardNo"]);
    string fraudType = Convert.ToString(e.NewValues["FraudType"]);
    string region = Convert.ToString(e.NewValues["Region"]);
    DateTime transactionDate = Convert.ToDateTime(e.NewValues["TransactionDate"]);
    decimal transactionAmount = Convert.ToDecimal(e.NewValues["TransactionAmount"]);
    ...
    
    gvSubmittedCases.EditIndex = -1;
    CaseView();
}


You'll want to set the grid's DataKeyNames to the name of the primary key, and extract that from the e.Keys dictionary.

You should then be able to call your stored procedure to update the record.

To change the "fraud type" column to a drop-down list, you'll need to use a TemplateField:
Walkthrough: Displaying a Drop-Down List While Editing in the GridView Web Server Control[^]


这篇关于使用boundfield更新gridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆