如何把数据写入DataGrid中去?

主  题:  如何把数据写入DataGrid中去?
作  者:  superman7826 (不说不知道)
等  级:  ^
信 誉 值:  94
所属论坛:  .NET技术 C#
问题点数:  100
回复次数:  9
发表时间:  2003-8-20 18:23:07

如何把数据写到DataGrid中,给点代码吧!
同时,在C#下面,如何判断字符串是数值形的呢?谢谢!



回复人: net_lover(孟子E章) ( 五星(高级)) 信誉:115 2003-8-20 18:27:50 得分:20

http://xml.sz.luohuedu.net/xml/Content.asp

回复人: hq1305018(跃强) ( 四级(中级)) 信誉:100 2003-8-20 18:38:35 得分:5

1、你首先建立一个DataRow对象,把数据添加到该对象中。DataGrid.Rows.Add方法可以增加新行。
2、

String aa="12345a";
try
{
    Console.WriteLine(Convert.ToInt32(aa));
}
catch(Exception ex)
{
    Console.Write("格式不正确");
}

回复人: snof(雪狼) ( 两星(中级)) 信誉:105 2003-8-20 19:39:50 得分:15

5.12 How can I restrict the keystrokes that will be accepted in a column of my datagrid?

You can create a custom column style and handle the KeyPress event of its TextBox member. Below is the code showing how this might be done. You can also download a sample project (C#, VB) that shows an implementation of this idea.

public class DataGridDigitsTextBoxColumn : DataGridTextBoxColumn
{
    public DataGridDigitsTextBoxColumn(System.ComponentModel.PropertyDescriptor pd, string format, bool b) : base(pd, format, b)
    {
        this.TextBox.KeyPress += new System.Windows.Forms.KeyPressEventHandler(HandleKeyPress);
    }

    private void HandleKeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) 
    {
        //ignore if not digit or control key
        if(!char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar))
            e.Handled = true;

        //ignore if more than 3 digits 
        if(this.TextBox.Text.Length >= 3 && !char.IsControl(e.KeyChar))
            e.Handled = true;
    }

    protected override void Dispose(bool disposing)
    {
        if(disposing)
            this.TextBox.KeyPress -= new System.Windows.Forms.KeyPressEventHandler(HandleKeyPress);
        base.Dispose(disposing);
    }
}

回复人: snof(雪狼) ( 两星(中级)) 信誉:105 2003-8-20 19:41:22 得分:10

或者从这儿下一个例子程序
http://www.syncfusion.com/FAQ/WinForms/FAQ_c44c.asp#q773q


5.16 How can I do cell by cell validation in a datagrid?

There are problems trying to implement cell by cell validation using the grid's Validating event architecture. The problem is that the grid is not the object handling the data. Instead, a TextBox or some other control is the control managing the changing of the cell contents. One way to implement the validation at the grid level is to handle the CurrentCellChanged event, and if the previous cell's value is not proper, then return to that cell. You can download a sample that implements this process. The sample only handles the validation from cell to cell movement. If you want to handle the validation when the user clicks on the forms Close button, then you would have to add a special event handler for this and do one last validation at this point.

回复人: Amilsx(一个人住) ( 五级(中级)) 信誉:100 2003-8-20 22:19:35 得分:0

To : net_lover(孟子E章) ( ) 信誉:115

你的网站的美工真该找个专业点的人来给你改改.挺难看的.................

呵呵

回复人: alphawin(云和山的彼端) ( 五级(中级)) 信誉:100 2003-8-21 8:46:36 得分:10

DataGrid.Rows.Add就可以了

回复人: wf5360308(峰) ( 二级(初级)) 信誉:100 2003-8-21 10:40:54 得分:25

很简单的,看一下代码就会了

DataRow a=dataSet31.Tables["表名"].NewRow();
a[0]=textBox1.Text;
a[1]=textBox2.Text;
a[2]=comboBox1.Text;
a[3]=textBox3.Text;
a[4]=dateTimePicker1.Text;
a[5]=dateTimePicker2.Text;
a[7]=comboBox2.Text;
a[6]=dateTimePicker3.Text;
a[8]=textBox4.Text;
a[9]=textBox5.Text;
a[10]=textBox6.Text;

dataSet31.Tables["表名"].Rows.Add(a);

sqlDataAdapter1.Update(dataSet31,"表名");

回复人: gatr() ( 四级(中级)) 信誉:98 2003-8-21 10:49:41 得分:5

直接修改dataset。

回复人: kuangren(今天逃课~) ( 三级(初级)) 信誉:100 2003-8-21 11:04:23 得分:10

Table.Rows.Add()就可以了~
wf5360308(峰) 那里已经说了很清楚了!

该问题已经结贴 ,得分记录: net_lover (20)、 hq1305018 (5)、 snof (15)、 snof (10)、 alphawin (10)、 wf5360308 (25)、 gatr (5)、 kuangren (10)、

Contributors: FHL