哎,感觉csdn上肯开尊口回答问题的人越来越少了,今天我连这个帖子共发了3个帖子,总共也只有3个人睬我,惨啊!!
解决方案 »
- DATATABLE 导出EXCEL 格式问题
- C# 子线程中timer失效的问题
- 未能加载文件或程序集“dotnetCHARTING
- C# WinForm如何把DataGridView的数据导出到Word?
- SetApartmentState
- DataGrid的ItemTemplate和BoundColumn问题!!!在线等待
- 怎么样才能像photoshop一样把绘画在窗体上的图保存为JPG,GIF 或者其他各式
- 谁做过打印物品不规则条码的软件,给点意见,最好有原代码,谢谢
- 急!急!请教高手,如何用window自带的notepad进行带参运行,实现文字查询,替换功能?
- signalr2客户端调用几次后就报错
- DBGrid 控件中有没有可以利用下拉式的功能 加快用户的输入 例如“省份”等等,急切?其他控件呢?
- c#的命名规范,大家来说说自己的经验。
-----
private DataSet myDataSet; //定义一个DATASETprivate void MakeDataSet()
{
// 建一个DataSet.
myDataSet = new DataSet("myDataSet");
//建立1个 DataTables.
DataTable TTABLE = new DataTable("查询表");
// 建1个columns, 和增加第一个 table.DataColumn ID = new DataColumn("类型",typeof(int));TTABLE.Columns.Add(ID);
// Add the tables to the DataSet.
//增加一表到DataSet
myDataSet.Tables.Add(TTABLE);DataRow newRow1;newRow1 = TTABLE.NewRow();
newRow1["ID"]=1;
TTABLE.Rows.Add(newRow1);
}private void loadgrid()
{//将GRID与DATASET绑上。
DataView dv=new DataView(myDataSet.Tables["查询表"]);
//dv.AllowNew=false;//这一句使得不能添加新列。
//dv.AllowDelete = false;
datagrid.DataSource=dv;
}
//好了,这样就可以用dataGrid1[0,0]=1;给这东东给值了
//还一点要提醒你的,dataGrid1[ROW,COL],COL和ROL不要超过索引值,否则会出错,如果你有需要增加多一行,可能用myDataSet.Tables["TTABLE"].NewRow ();//增加一行!!//好了,晚安~~噢,是早安了~
using System.Data ;
//------
private DataSet myDataSet; //定义一个DATASET
//------
private void MakeDataSet()
{
// 建一个DataSet.
myDataSet = new DataSet("myDataSet");
//建立1个 DataTables.
DataTable TTABLE = new DataTable("TTABLE");
// 建1个columns, 和增加第一个 table.
DataColumn CID = new DataColumn("ID",typeof(int));
TTABLE.Columns.Add(CID);
// Add the tables to the DataSet.
//增加一表到DataSet
myDataSet.Tables.Add(TTABLE);
//增加一行.
DataRow newRow1;
newRow1 = TTABLE.NewRow();
newRow1["ID"]=1;
TTABLE.Rows.Add(newRow1);
}
//将GRID与DATASET绑上。
private void loadgrid()
{
DataView dv=new DataView(myDataSet.Tables["TTABLE"]);
//dv.AllowNew=false;//这一句使得不能添加新列。
//dv.AllowDelete = false;
datagrid.DataSource=dv;
}
//好了,这样就可以用dataGrid1[0,0]=1;给这东东给值了
//还一点要提醒你的,dataGrid1[ROW,COL],COL和ROL不要超过索引值,否则会出错,如果你有需要增加多一行,可能用myDataSet.Tables["TTABLE"].NewRow ();//增加一行!!
十二万分的感谢你回答我的问题,这个问题我已经解决了
另外想问你有没有给dataGrid控件某列上加个comboBox控件的例子??
我看了以前的帖子,好象说要重写dataGridColumntStyle,但都说的不清楚,
而在www.csharphlep.com上找到的例子错漏百出,如你能给我个例子,我将不胜感激!!
//This class describes the method to add any controls of your choice to the data
// grid control.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Text;
using System.Xml;
using System.Data.SqlClient;
namespace PWDgFunctionalArea
{
public class TestDataGrid : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variables
/// </summary>
private System.Windows.Forms.ComboBox comboControl;
private DataTable dataTable;
private System.Windows.Forms.Button button1;
private DataGrid.HitTestInfo hitTestGrid;
private System.Windows.Forms.DataGrid datagrid1;
private DataGridTextBoxColumn datagridtextBox;
private string[] arrstr ; /// <summary>
/// public constructor
/// </summary>
public TestDataGrid()
{
InitializeComponent();
//Method to create the customized data grid
CreateGrid();
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
static void Main()
{
Application.Run(new TestDataGrid());
}
/// <summary>
/// Method defn to create the Data Grid using data table
/// </summary>
private void CreateGrid()
{
//Declare and initialize local variables used
DataColumn dtCol = null;//Data Column variable
//Create the String array object, initialize the array with the column
//names to be displayed
arrstr = new string [3];
arrstr[0] = "Control Name";
arrstr[1] = "Control";
arrstr[2] = "Description";
//Create the Data Table object which will then be used to hold
//columns and rows
dataTable = new DataTable("Controls");
//Add the string array of columns to the DataColumn object
for(int i=0; i< 3;i++)
{
string str = arrstr[i];
dtCol = new DataColumn(str);
dtCol.DataType = System.Type.GetType("System.String");
dtCol.DefaultValue = "";
dataTable.Columns.Add(dtCol);
}
DataRow newRow1;
newRow1=dataTable.NewRow ();
newRow1["Control Name"]="ComboBox";
newRow1["Control"]="请单击这里看下拉的效果!!!";
dataTable.Rows.Add(newRow1);
newRow1=dataTable.NewRow ();
newRow1["Control Name"]="DateTime";
dataTable.Rows.Add(newRow1);
newRow1=dataTable.NewRow ();
newRow1["Control Name"]="CheckBox";
dataTable.Rows.Add(newRow1);
//Set the Data Grid Source as the Data Table created above
datagrid1.DataSource = dataTable;
//set style property when first time the grid loads, next time onwards it //will maintain its property
if(!datagrid1.TableStyles.Contains("Controls"))
{
//Create a DataGridTableStyle object
DataGridTableStyle dgdtblStyle = new DataGridTableStyle();
//Set its properties
dgdtblStyle.MappingName = dataTable.TableName;//its table name of dataset
datagrid1.TableStyles.Add(dgdtblStyle);
dgdtblStyle.RowHeadersVisible = false;
dgdtblStyle.HeaderBackColor = Color.LightSteelBlue;
dgdtblStyle.AllowSorting = false;
dgdtblStyle.HeaderBackColor = Color.Navy;//.FromArgb(8,36,107);
dgdtblStyle.HeaderForeColor = Color.White;
dgdtblStyle.HeaderFont = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
dgdtblStyle.GridLineColor = Color.DarkGray;
dgdtblStyle.PreferredRowHeight = 22;
datagrid1.BackgroundColor = Color.White; //Take the columns in a GridColumnStylesCollection object and set //the size of the
//individual columns
GridColumnStylesCollection colStyle;
colStyle = datagrid1.TableStyles[0].GridColumnStyles;
colStyle[0].Width = 97;
colStyle[1].Width = 220;
colStyle[2].Width = 150;
}
//Take the text box from the second column of the grid where u will be adding the controls of your choice
datagridtextBox = (DataGridTextBoxColumn)datagrid1.TableStyles[0].GridColumnStyles[1];
datagridtextBox.TextBox.GotFocus += new EventHandler(this.dgdFunctionArea_GotFocus);
}
/// <summary>
/// Method defn to connect to the data base and then load the data grid
/// </summary>
{
MessageBox.Show ("如果要读数库,请改到下面的代码!!");
return;
try
{
//Establish the connection to the data base and open it
SqlConnection sqlConn = new SqlConnection("user id=sa;password=;initial catalog=northwind;data source=mySQLServer;Connect Timeout=30");
//*-Pass the required details
sqlConn.Open();
//create the sql command object and set its command type to execute the sql query to get the results
SqlCommand sc = new SqlCommand();
sc.Connection = sqlConn;
sc.CommandType = CommandType.Text;
sc.CommandText = "SELECT * FROM test";
//create the data set object to be used to fill the data grid with the data
DataSet ds = new DataSet();
//Create the sql adapter that will be used to fill the data set created above
SqlDataAdapter myReader = new SqlDataAdapter(sc);
myReader.Fill(ds); //Fill the rows in the grid
for(int i =0;i<ds.Tables[0].Rows.Count;i++)
{
dataTable.LoadDataRow(arrstr,true);
datagrid1[i,0] = ds.Tables[0].Rows[i].ItemArray[0].ToString();
datagrid1[i,1] = ds.Tables[0].Rows[i].ItemArray[1].ToString();
datagrid1[i,2] = ds.Tables[0].Rows[i].ItemArray[2].ToString();
}
}
catch(Exception)
{}
} /// <summary>
/// to create the controls
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.datagrid1 = new System.Windows.Forms.DataGrid();
((System.ComponentModel.ISupportInitialize)(this.datagrid1)).BeginInit();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(246, 176);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(96, 26);
this.button1.TabIndex = 9;
this.button1.Text = "Load Grid";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// datagrid1
//
this.datagrid1.BackgroundColor = System.Drawing.SystemColors.ActiveCaptionText;
this.datagrid1.CaptionVisible = false;
this.datagrid1.DataMember = "";
this.datagrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.datagrid1.Location = new System.Drawing.Point(10, 19);
this.datagrid1.Name = "datagrid1";
this.datagrid1.PreferredRowHeight = 20;
this.datagrid1.ReadOnly = true;
this.datagrid1.Size = new System.Drawing.Size(604, 148);
this.datagrid1.TabIndex = 10;
this.datagrid1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.dgd_MouseDown);
//
// TestDataGrid
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(624, 209);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.datagrid1,
this.button1});
this.Name = "TestDataGrid";
this.Load += new System.EventHandler(this.TestDataGrid_Load);
((System.ComponentModel.ISupportInitialize)(this.datagrid1)).EndInit();
this.ResumeLayout(false); }
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
/// <summary>
/// Method defn to add the control of your choice to the data grid
/// </summary>
/// <param name="o"></param>
/// <param name="e"></param>
private void dgdFunctionArea_GotFocus(object o, EventArgs e)
{
//Create the combo control to be added and set its properties
comboControl = new ComboBox();
comboControl.Cursor = System.Windows.Forms.Cursors.Arrow;
comboControl.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDown;
comboControl.Dock = DockStyle.Fill;
comboControl.Items.AddRange(new string[5]{"测试这里!!!!哈哈哈","Information Technology","Computer Science","Bio Technology","Electrical Engg"}); //Create the date time picker control to be added and set its properties
DateTimePicker dtp = new DateTimePicker();
dtp.Dock = DockStyle.Fill;
dtp.Cursor = Cursors.Arrow; //Create the check box control to be added and set its properties
CheckBox chk = new CheckBox();
chk.Dock = DockStyle.Fill;
chk.Cursor = Cursors.Arrow; //Create the radio button control to be added and set its properties
RadioButton rb = new RadioButton();
rb.Dock = DockStyle.Fill;
rb.Cursor = Cursors.Arrow;
//Add the controls to the respective columns in the data grid
for(int i = 0 ;i < dataTable.Rows.Count ; i++)
{
//if the data in the first column is date time, add a date time control to the grid
if(datagrid1[i,0].ToString().Equals("DateTime") && hitTestGrid != null && hitTestGrid.Row == i)
{
datagridtextBox.TextBox.Controls.Add(dtp);
comboControl.SendToBack();
chk.SendToBack();
rb.SendToBack();
dtp.BringToFront();
}
//if the data in the first column is combo box, add a combo box control to the grid
else if(datagrid1[i,0].ToString().Equals("ComboBox") && hitTestGrid != null && hitTestGrid.Row == i)
{
datagridtextBox.TextBox.Controls.Add(comboControl);
chk.SendToBack();
dtp.SendToBack();
rb.SendToBack();
comboControl.BringToFront();
comboControl.Text =datagrid1[datagrid1.CurrentCell.RowNumber,datagrid1.CurrentCell.ColumnNumber].ToString ();
comboControl.TextChanged += new EventHandler(this.comboControl_TextChanged);
}
//if the data in the first column is check box, add a check box control to the grid
else if(datagrid1[i,0].ToString().Equals("CheckBox") && hitTestGrid != null && hitTestGrid.Row == i)
{
datagridtextBox.TextBox.Controls.Add(chk);
comboControl.SendToBack();
dtp.SendToBack();
rb.SendToBack();
chk.BringToFront();
}
//if the data in the first column is radio button, add a radio button control to the grid
if(datagrid1[i,0].ToString().Equals("Radio Button") && hitTestGrid != null && hitTestGrid.Row == i)
{
datagridtextBox.TextBox.Controls.Add(rb);
comboControl.SendToBack();
chk.SendToBack();
dtp.SendToBack();
rb.BringToFront();
}
datagridtextBox.TextBox.BackColor = Color.White;
}
}
private void comboControl_TextChanged(object sender, System.EventArgs e)
{
datagrid1[datagrid1.CurrentCell.RowNumber,datagrid1.CurrentCell.ColumnNumber]=comboControl.Text; //将改变的值传回DATAGRID中.
}
/// <summary>
/// MEthod defn when load grid button is clicked
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, System.EventArgs e)
{
try
{
LoadGrid();
}
catch{}
}
/// <summary>
/// Mouse down event of the data grid
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void dgd_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
//take the hit test that will be used to identify which row has been clicked
hitTestGrid = datagrid1.HitTest(e.X,e.Y);
} private void TestDataGrid_Load(object sender, System.EventArgs e)
{
}
}//end of the class
}//end of the namespace
另dzq138(钟添)真是个好人,会有好报的,哈哈
喂,我叫dzj,跟你差不多哦