欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

c#中datagridview处理非绑定列的方法

程序员文章站 2024-02-06 11:27:40
本文实例讲述了c#中datagridview处理非绑定列的方法。分享给大家供大家参考。具体实现方法如下: using system; using system....

本文实例讲述了c#中datagridview处理非绑定列的方法。分享给大家供大家参考。具体实现方法如下:

using system;
using system.collections.generic;
using system.componentmodel;
using system.data;
using system.drawing;
using system.text;
using system.windows.forms;
using datagridview1.dataset1tableadapters;
namespace datagridview1
{
 public partial class form1 : form
 {
  public form1()
  {
   initializecomponent();
  }
  private void form1_load(object sender, eventargs e)
  {
   customerstableadapter adapter = new customerstableadapter();
   bindingsource1.datasource = adapter.getdata();
   datagridview1.autogeneratecolumns = false;
   int newcolindex = datagridview1.columns.add("companyname", "companyname");
   datagridview1.columns[newcolindex].datapropertyname = "companyname";
   newcolindex = datagridview1.columns.add("contactname", "contactname");
   datagridview1.columns[newcolindex].datapropertyname = "contactname";
   newcolindex = datagridview1.columns.add("phone", "phone");
   datagridview1.columns[newcolindex].datapropertyname = "phone";
   newcolindex = datagridview1.columns.add("contact", "contact");
   datagridview1.cellformatting += oncellformatting;
   datagridview1.datasource = bindingsource1;
  }
  private void oncellformatting(object sender, datagridviewcellformattingeventargs e)
  {
   if (e.columnindex == datagridview1.columns["contact"].index)
   {
    e.formattingapplied = true;
    datagridviewrow row = datagridview1.rows[e.rowindex];
    e.value = string.format("{0}:{1}", row.cells["contactname"].value, row.cells["phone"].value);
   }
  }
 }
}

希望本文所述对大家的c#程序设计有所帮助。