在asp.net gridview中编辑单个单元格 [英] Edit single cell in asp.net gridview

查看:72
本文介绍了在asp.net gridview中编辑单个单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

这些天来,我在编辑/更新gridview行中的单元格时遇到了一个大问题.过去两天,我一直在浏览所有相关资源(包括本网站),但没有找到任何解决方案.所以,请,如果您知道-帮帮我:)

我的gridview(名为gv_korpa)绑定到Controller中的linq对象查询.
因此,让我们从那里开始:


Hello everyone,

I have a big issue these days regarding to editing/ updating cell in a gridview row. Past two days I`ve been browsing all relevant sources (including this site), but I haven`t found any solution. So, please, if You know- help me :)

My gridview (named gv_korpa) is bound to linq object query which is in Controller.
So, let`s go from there:


public static Object prikaziKorpu(string korpa)
        {
            var upit = (from x in DCKlase.dc.Kartas
                        where x.cartID==korpa
                        select new
                        {
                           
                            x.Id,
                            Artikal=x.eArtikli.naziv,
                            IDa=x.artikalFK,
                            Datum=x.datum,
                            Količina= x.kolicina,
                            Proizvođač=x.eArtikli.eProizvodaci.naziv,
                            Vrsta=x.eArtikli.eVrste.naziv,
                            Oblik=x.eArtikli.oblik,
                            Participacija=x.eArtikli.participacija,
                            Opis=x.eArtikli.opisArtikla,
                            Cijena= x.eArtikli.cijena,
                            CARD=x.cartID,
                            UKUPNO= (x.eArtikli.cijena * x.kolicina)-x.eArtikli.participacija,
                          
                         
                            
                        }).ToList();
            return upit;
            
        }


Gridview绑定在Page_Load上,如下所示:


Gridview is bound on Page_Load, like this:

eShop es = new eShop();
            String cartID = es.getShoppingCartId();
            if (!IsPostBack)
            {
                gv_korpa.DataSource = KorpaController.prikaziKorpu(Session["PrijavljeniKorisnik"].ToString());
                gv_korpa.DataBind();
            }
            lbl_ukupno.Text = String.Format("{0:c}", es.getTotal(cartID));



并正确绑定(一切都应显示为原样).

后面的Asp.net代码如下所示:




and it bound correctly (everything is shown as it should be).

Asp.net code behind look like this:


<asp:GridView ID=gv_korpa runat="server"  Width="438px" 
                AutoGenerateColumns="False" onrowdeleting="gv_korpa_RowDeleting" 
                AutoGenerateEditButton="True" onrowediting="gv_korpa_RowEditing" 
                onrowupdating="gv_korpa_RowUpdating" onrowcancelingedit="gv_korpa_RowCancelingEdit"
                 >
                <Columns>
               <asp:BoundField DataField="Id" HeaderText="ID" Visible="true"/>
               <asp:BoundField DataField="Artikal" HeaderText="Artikal"/> 
               <asp:BoundField DataField="Proizvođač" HeaderText="Proizvođač"/> 
               <asp:BoundField DataField="Vrsta" HeaderText="Vrsta"/>
               <asp:BoundField DataField="Oblik" HeaderText="Oblik preparata"/>
               <asp:BoundField DataField="Cijena" HeaderText="Cijena"/>
             <asp:BoundField DataField="Količina"/>
              <asp:BoundField DataField="Participacija" HeaderText="Participacija ZZZO"/>
               <asp:BoundField DataField="UKUPNO" HeaderText="UKUPNO"/>
               <asp:BoundField DataField="CARD" HeaderText="Korisnik" Visible="false"/>   
               <asp:BoundField DataField="IDa" />
               <asp:BoundField DataField="Datum" />             
               <asp:CommandField ButtonType="Button" DeleteText="Ukloni" ShowDeleteButton="True"  />
   
                </Columns>
          
            </asp:GridView>



我已经在事件row_deleting上实现了delete方法,并且效果很好:



I`ve implemented delete method on event row_deleting and it works fine:

protected void gv_korpa_RowDeleting(object sender, GridViewDeleteEventArgs e)
       {
           int Id = int.Parse(gv_korpa.Rows[e.RowIndex].Cells[0].Text);
           KorpaController.oznacenaKarta(Id);
           eShop es = new eShop();
           String cartID = es.getShoppingCartId();


           gv_korpa.DataSource = KorpaController.prikaziKorpu(Session["PrijavljeniKorisnik"].ToString());
           gv_korpa.DataBind();
           lbl_ukupno.Text = String.Format("{0:c}", es.getTotal(cartID));
       }



但是,更新行单元格不起作用!我想要的是将点击的数量更新1.
我试图将数量单元格更改为文本字段,但这也没有用.
这是我已经实现的更新方法(由于我在其他控件上使用它,因此可以100%工作).




But, update row cell is not working! What I wanted is to update my quantity by 1 on click.
I`ve tryed to change the quantity cell to textfield, but that also didn`t worked.
Here is update method which I`ve implemented (it working 100% because I using it on the other control).


public void AddItem(int ProductID, int quantity)
        {

            using (DCKlaseDataContext dc = new DCKlaseDataContext())
            {
                try
                {
                    var myItem = (from c in dc.Kartas
                                  where c.Id== ProductID

                                  select c).FirstOrDefault();

                    if (myItem == null)
                    {


                        


                    }
                    else
                    {
                        myItem.kolicina += quantity;
                    }
                    dc.SubmitChanges();

                }
                catch (Exception exp)
                {
                    //Implement exception handling
                }
            }



那么,有什么想法吗?非常感谢您的帮助!

此致

A.



So, any ideas?! I would really appreciate Your help!

Regards,

A.

推荐答案

再次绑定
gv_korpa.DataBind();
bind it again
gv_korpa.DataBind();


嗨ellisbay,

您要在网格视图中编辑单个单元格,因此需要执行一些步骤:

1)首先在网格视图中获取一个链接,该链接将为Edit

2)在DataKeyNames中绑定您在gridview中使用的ID,在Edit Link.CommandName ="EDI"
的Command参数中使用该ID
3)在网格视图的行命令"中输入代码,即"gridview1_row"命令.

4)使类以任何您想编辑的方式调用您的过程或内联查询.调用您将在网格视图中选择行的ID.

5)在.aspx页面上使用以下代码显示为:根据需要更改以下字段

Hi ellisbay,

You want to Edit single cell in grid view so for this you need to follow some steps:

1) first take a link in grid view whose would be Edit

2) bind The Id you are using in gridview in DataKeyNames,Use that Id in Command argument of Edit Link.CommandName="EDI"

3) give your code at Row Command of your grid view that is gridview1_row command.

4)Make class Call your procedure or inline queries,by whatever means you would like to edit. call the Id by which you will select the row in grid view.

5)Use below code on .aspx page show as: change below fields according to needs

if (e.CommandName == "EDI")
        {
            Int32 ModuleId = Convert.ToInt32(e.CommandArgument.ToString());
            ViewState["ModuleId"]=ModuleId;
            DataTable dt = new DataTable();
            dt=Module.editgridModule(ModuleId);
            if (dt.Rows.Count > 0)
            {
                ddlProjectName.SelectedValue = dt.Rows[0]["ProjectId"].ToString());
                fillModuleDropdown();
                txtModuleName.Text = dt.Rows[0]["ModuleName"].ToString();
                txtModuleNameAbri.Text = dt.Rows[0]["ModuleCode"].ToString();
                txtsignoff.Text=dt.Rows[0]["SignoffDate"].ToString();
            }
}


在Update中,还将数据再次绑定到网格.
那就是唯一缺少的东西.
In Update also bind the data to grid again.
Thats the only thing missing.


这篇关于在asp.net gridview中编辑单个单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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