private void FTPTestForm_Load(object sender, EventArgs e)
{
BindingSource bs = null;
DataTable dtData = GetData();//dtData(ID,Name,Sex);
bs = new BindingSource();
bs.DataSource = dtData;
this.dataGridView1.DataSource = bs;
} private DataTable GetData()
{
DataTable table = new DataTable();
DataColumn column;
DataRow row; column = new DataColumn();
column.DataType = System.Type.GetType("System.String");
column.ColumnName = "Sex";
column.ReadOnly = true;
column.Unique = true;
// Add the Column to the DataColumnCollection.
table.Columns.Add(column); // Create second column.
column = new DataColumn();
column.DataType = System.Type.GetType("System.String");
column.ColumnName = "Name";
column.AutoIncrement = false;
column.Caption = "Name";
column.ReadOnly = false;
column.Unique = false;
table.Columns.Add(column); DataColumn[] PrimaryKeyColumns = new DataColumn[1];
PrimaryKeyColumns[0] = table.Columns["id"];
table.PrimaryKey = PrimaryKeyColumns; for (int i = 0; i < 2; i++)
{
row = table.NewRow();
if(i==0)
row["Sex"] = "女";
if(i==1)
row["Sex"] = "男";
row["Name"] = "Name " + i;
table.Rows.Add(row);
}
return table;
}
参考!
解决方案 »
- 获取鼠标双击时间间隔问题,不论鼠标是否双击或双击速度,程序运行时始终显示时间为500毫秒,谢谢大家帮忙
- 关于打开窗体
- winfrom或者ASP.NET 做windows桌面效果
- 如何让datagridview中的单行显示一种颜色,偶行显示另一种颜色???
- 关于C#加载组件的问题?
- 怎么在C# Silverlight项目MainPage.xaml.cs中写入文件保存到本地,保存到本地的路径是绝对路径(如保存到C:\aaa.txt),怎么写?
- 关于背景图片的问题,请大家进来看看
- 请问怎么把字符串转换成16进制的数啊急啊,马上给分
- SqlServer中存储一段视频怎么操作?
- 一个小小的问题!
- 关于键盘侦测的问题,急,在线等待!
- 如何实现 可逆加密
if(i==0)
row["Sex"] = "女";
if(i==1)
row["Sex"] = "男";
主要是这里
{
if (e.ColumnIndex == SexColumn.Index)
if (e.Value == 0)
e.Value = "男";
else if (e.Value == 1)
e.Value = "女";
else if (e.Value == 2)
e.Value = "人妖";
else
e.Value = String.Empty;
}
有没有仔细看过我的代码啊。Cell_Formatting事件是在datagridview的单元格值显示前的格式化处理时的时候触发的,是确定取出的数据具体怎么去显示。而不会修改bs的原始数据
你说的不对,bs中的数据是要被更改为“男”这个值的,不信你自己测测,而我的期望是被修改为1.而不是修改为男。 意思就是说
从数据库取出两个人放到bs中
A(nameA,25,1):
B(nameB,23,0);
绑定到datagridview显示为
nameA 25 男
nameB 23 女现在把nameA改为女以后期望bs中得到
nameA,25,0这个结果,然而实际bs中得到的结果是
nameA,25,女。
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace WindowsApplication41
{
public partial class Form1 : Form
{
DataTable DT = null;
public Form1()
{
InitializeComponent();
this.Controls.Clear();
this.StartPosition = FormStartPosition.CenterScreen;
DT = new DataTable();
DT.Columns.Add(new DataColumn("sex"));
DT.Rows.Add(new Object[] { 0 });
DT.Rows.Add(new Object[] { 1 });
DataGridView DGV = new DataGridView();
DGV.Parent = this;
DGV.Dock = DockStyle.Fill;
DataGridViewTextBoxColumn DGVC = new DataGridViewTextBoxColumn();
DGVC.DataPropertyName = "sex";
DGV.Columns.Add(DGVC);
DGV.CellFormatting += new DataGridViewCellFormattingEventHandler(DGV_CellFormatting);
DGV.DataSource = DT; Button B = new Button();
B.Text = "查看值";
B.Parent = this;
B.Click += new EventHandler(B_Click);
B.BringToFront();
} void B_Click(object sender, EventArgs e)
{
MessageBox.Show(DT.Rows[0][0].ToString() + " " + DT.Rows[1][0].ToString()); // 输出: 0 1
} void DGV_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (Convert .ToInt32 ( e.Value) == 0)
e.Value = "男";
else if (Convert .ToInt32 (e.Value )== 1)
e.Value = "女";
else
e.Value = String.Empty;
}
}
}