当一个单元格上的索引发生更改时,如何更新Datagridview单元格 [英] How Do I Update A Datagridview Cell When Index On One Cell Changes

查看:62
本文介绍了当一个单元格上的索引发生更改时,如何更新Datagridview单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在忙于一个系统而且我已经在页面中添加了一个datagridview。



假设我的数据库中有3个coulumns称为

ItemCode

描述

价格



我想做的是当用户输入datagridview中itemcode列中的代码

描述和价格应从链接数据库表中提取数据并在Description单元格和价格单元格中显示。

< br $>
EG



商品编号为4500



描述

手机



价格

3500





Datagridview



ItemCode描述价格

4500手机(显示标签/焦点丢失的商品代码字段)3500(至显示一次tabbed off itemcode字段)

I am currently busy on a system and i have added a datagridview to the page.

Lets say i have 3 coulumns in my database called
ItemCode
Description
Price

what i want to do is when the user enters a code in the itemcode column in the datagridview
the description and the price should pull the data from the linked database tables and show it in the Description cell and price cell.

E.G

Item code is 4500

Description
Cellphone

Price
3500


Datagridview

ItemCode Description Price
4500 Cellphone(to show once tabbed/focus lost off the itemcode field) 3500(to show once tabbed off the itemcode field)

推荐答案

这个我s示例代码结构

你可以扩展到你的需要..

key CellEndEdit 活动





This is the sample code structure
you can extend to your need..
key is the CellEndEdit event


private void Form1_Load(object sender, EventArgs e)
      {
          DataTable dt = new DataTable();
          dt.Columns.Add("ItemCode", typeof(int));
          dt.Columns.Add("Description", typeof(string));
          dt.Columns.Add("Price", typeof(double));
          dt.Rows.Add(1, "some", 43);
          dgv_contents.DataSource = dt;
      }



      private void dgv_contents_CellEndEdit(object sender, DataGridViewCellEventArgs e)
      {
          if (e.ColumnIndex == 0)
          {
              string ItemCode = dgv_contents.Rows[e.RowIndex].Cells[e.ColumnIndex].Value + "";
              dgv_contents.Rows[e.RowIndex].Cells[1].Value = GetDescription(ItemCode);
              dgv_contents.Rows[e.RowIndex].Cells[2].Value = Getprice(ItemCode);

          }
      }

      public string GetDescription(string value)
      {
          // get the data  form your db or some source
          return "some descrition";
      }
      public double Getprice(string value)
      {
          // get teh data from db or some external source..
          return 98;
      }


这篇关于当一个单元格上的索引发生更改时,如何更新Datagridview单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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