c#中datagridview處理非綁定列的方法
更新時間:2015年06月20日 11:58:48 作者:zhuzhao
這篇文章主要介紹了c#中datagridview處理非綁定列的方法,實(shí)例分析了C#中datagridview的使用技巧,需要的朋友可以參考下
本文實(shí)例講述了c#中datagridview處理非綁定列的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
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#程序設(shè)計(jì)有所幫助。
相關(guān)文章
C#實(shí)現(xiàn)Word轉(zhuǎn)為PDF的方法
今天小編就為大家分享一篇關(guān)于C#實(shí)現(xiàn)Word轉(zhuǎn)為PDF的方法,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-01-01
C#命令模式(Command Pattern)實(shí)例教程
這篇文章主要介紹了C#命令模式(Command Pattern),以實(shí)例的形式講述了命令模式通過一個指令來控制多個類的多個方法,需要的朋友可以參考下2014-09-09
C#動態(tài)創(chuàng)建Access數(shù)據(jù)庫及密碼的方法
同為微軟的產(chǎn)品,本文將討論的是C#如何創(chuàng)建Access數(shù)據(jù)庫,同時創(chuàng)建數(shù)據(jù)庫密碼與相關(guān)操作,希望對大家有所幫助。2015-09-09
C#和vb.net實(shí)現(xiàn)PDF 添加可視化和不可見數(shù)字簽名
本文通過C#程序代碼展示如何給PDF文檔添加可視化數(shù)字簽名和不可見數(shù)字簽名。文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-08-08

