English 中文(简体)
动态物体数据网细胞价值变化活动手
原标题:datagrid cellvaluechanged event handler for dynamic object

我正在从方案上建立一个数据网,我希望把一个从用户那里变化的单位的价值提高到另一个数据网。

我的守则是:

connTo.Open();

SqlDataReader sqlFromReader1 = null;
SqlDataReader sqlFromReader2 = null;
SqlDataReader sqlFromReader3 = null;

SqlCommand myFromCommand1 = new SqlCommand("select distinct m.catid1, c.descr "+
"from material m "+
"inner join cat1 c on c.id = m.catid1", connTo);
sqlFromReader1 = myFromCommand1.ExecuteReader();

DataTable dt1 = new DataTable();
dt1.Load(sqlFromReader1);

for (i = 0; i < dt1.Rows.Count; i++)
{
    //int c1 = (int)(dt1.Rows[i]["catid1"]);
    TabPage tbg = new TabPage(dt1.Rows[i]["descr"].ToString());
    tabControl1.Controls.Add(tbg);

    TabControl tbi = new TabControl();
    tbi.Name = i.ToString() + i.ToString();

    SqlCommand myFromCommand2 = new SqlCommand(
        "select distinct m.catid2, c.descr " +
        "from material m " +
        "inner join cat2 c on c.id = m.catid2 "+
        "and m.catid1 = " + dt1.Rows[i]["catid1"].ToString(), connTo);
        sqlFromReader2 = myFromCommand2.ExecuteReader();

    DataTable dt2 = new DataTable();
    dt2.Load(sqlFromReader2);

    for (int k = 0; k < dt2.Rows.Count; k++)
    {
        //int c2 = (int)(dt2.Rows[k]["catid2"]);
        TabPage tbgi = new TabPage(dt2.Rows[k]["descr"].ToString());
        tbi.Controls.Add(tbgi);
        tbi.Left = 1;
        tbi.Top = 1;
        tbi.Width = 880;
        tbi.Height = 400;


        DataGridView gv = new DataGridView();

        gv.Width = 850;
        gv.Height = 340;            
        gv.Left = 10;
        gv.Top = 10;
        gv.ColumnCount = 4;
        gv.Columns[0].Name = "code";
        gv.Columns[0].Width = 120;
        gv.Columns[0].ReadOnly = true;

        gv.Columns[1].Name = "name";
        gv.Columns[1].Width = 450;
        gv.Columns[1].ReadOnly = true;

        gv.Columns[2].Name = "price";
        gv.Columns[2].Width = 80;
        gv.Columns[2].ReadOnly = true;

        gv.Columns[3].Name = "qty";
        gv.Columns[3].Width = 120;

        SqlCommand myFromCommand3 = new SqlCommand(
            "select m.code, m.descr, m.cost1 price " +
            "from material m " +
            "where m.catid1 = " + dt1.Rows[i]["catid1"].ToString() + " " +
            "and m.catid2 = " + dt2.Rows[k]["catid2"].ToString(), connTo);

        sqlFromReader3 = myFromCommand3.ExecuteReader();

        DataTable dt3 = new DataTable();
        dt3.Load(sqlFromReader3);

        for (int l = 0; l < dt3.Rows.Count; l++)
        {
            gv.Rows.Add(dt3.Rows[l]["code"].ToString(), dt3.Rows[l]["descr"].ToString(), dt3.Rows[l]["price"].ToString(), "");
        }

        tbgi.Controls.Add(gv);                       
    }
    tbg.Controls.Add(tbi);

现在,当用户改变格夫·哥伦斯的价值时,我想赶上。 姓名(“qty”)在新数据清单中添加这一数值和其余数据。

如何做到这一点? 这是可能的吗?

问题回答

The CellValueChanged activity should work in this case.

每当你创建新的<条码>时 DataGridView, 例举,在表格中添加:

dataGridView1.CellValueChanged += new DataGridViewCellEventHandler(dataGridView1_CellValueChanged);

然后,请在单一地点使用<代码>数据GridView1_CellValueChanged,供所有活动处理者使用。

<代码>CellValueChanged, 活页码:,该编码对栏目和行程索引都有变更的财产。 因此,你有这样的守则:

void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
    if (e.ColumnIndex == 3)
    {
        // Now you can cast sender to a DataGridView and copy the row at e.RowIndex
        DataGridView dgv = sender as DataGridView;
        if (dgv == null)
            return;

        // Here you get the cell value
        var cellValue = dgv.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
    }
}

我还建议将dt3作为数据来源。 如果你确实使用数据来源,而不是使用一个无约束的电网,那么该行的复印将更清洁。





相关问题
Bring window to foreground after Mutex fails

I was wondering if someone can tell me what would be the best way to bring my application to the foreground if a mutex was not able to be created for a new instance. E.g.: Application X is running ...

How to start WinForm app minimized to tray?

I ve successfully created an app that minimizes to the tray using a NotifyIcon. When the form is manually closed it is successfully hidden from the desktop, taskbar, and alt-tab. The problem occurs ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

Handle DataTable.DataRow cell change event

I have a DataTable that has several DataColumns and DataRow. Now i would like to handle an event when cell of this DataRow is changed. How to do this in c#?

Apparent Memory Leak in DataGridView

How do you force a DataGridView to release its reference to a bound DataSet? We have a rather large dataset being displayed in a DataGridView and noticed that resources were not being freed after the ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

WPF-XAML window in Winforms Application

I have a Winforms application coded in VS C# 2008 and want to insert a WPF window into the window pane of Winforms application. Could you explain me how this is done.

热门标签