如何根据组合框列选择在datagridview中填充同一行中的两个文本框 [英] How can I fill two textboxes in the same row in datagridview based on combobox column selection

查看:96
本文介绍了如何根据组合框列选择在datagridview中填充同一行中的两个文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已更新并过滤了dgv中的两个组合框,但不知道如何填充文本框,我有一张桌子:



streetId,streetName,areaId,ISFsectionId1 ,ISFsectionId2;其中areaId和streetName是组合框,我将数据绑定到组合框并填充它但现在不知道填充isfsectionId这是我的代码:



i have updated the and filter a two comboboxes in dgv but dont know how fill the textboxes,i have a table :

streetId,streetName,areaId,ISFsectionId1,ISFsectionId2; where areaId and streetName are comboboxes,i made the data binding to the comboboxes and populate it but now dont know to fill the isfsectionId's this is my code:

public IncidentForm()  

{  
    tblPrimary = new DataTable();  
    SqlCommand cmd1 = cnn.CreateCommand();  
    cmd1.CommandText = "select * from area";  
    SqlDataAdapter sdr1 = new SqlDataAdapter(cmd1);  
    cmd1.CommandType = CommandType.Text;  
    cmd1.Connection = cnn;  
    sdr1.Fill(tblPrimary);  

    tblSecondary = new DataTable();  
    SqlCommand cmd2 = cnn.CreateCommand();  
    cmd2.CommandText = "select * from street";  
    SqlDataAdapter sdr2 = new SqlDataAdapter(cmd2);  
    cmd2.CommandType = CommandType.Text;  
    cmd2.Connection = cnn;  
    sdr2.Fill(tblSecondary);  


    InitializeComponent();  

    primaryBS = new BindingSource();  
    primaryBS.DataSource = tblPrimary;  
    Column15.DataSource = primaryBS;  
    Column15.DisplayMember = "areaName";  
    Column15.ValueMember = "areaId";  

    // the ComboBox column is bound to the unfiltered DataView  
    unfilteredSecondaryBS = new BindingSource();  

    DataView undv = new DataView(tblSecondary);  
    unfilteredSecondaryBS.DataSource = undv;  
    Column17.DisplayMember = "streetName";  
    Column17.ValueMember = "streetId";  
    Column17.DataSource = unfilteredSecondaryBS;  


    // this binding source is where I perform my filtered view  
    filteredSecondaryBS = new BindingSource();  
    DataView dv = new DataView(tblSecondary);  
    filteredSecondaryBS.DataSource = dv;  
}  

private void dataGridView7_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)  
{  
    if (e.ColumnIndex == Column17.Index)  
    {  
        DataGridViewComboBoxCell dgcb = (DataGridViewComboBoxCell)dataGridView7[e.ColumnIndex, e.RowIndex];  

        dgcb.DataSource = filteredSecondaryBS;  
        this.filteredSecondaryBS.Filter = "areaId = " +  
            this.dataGridView7[e.ColumnIndex -1 , e.RowIndex].Value.ToString();  
    }
}  

private void dataGridView7_CellEndEdit(object sender, DataGridViewCellEventArgs e)  
{  
    if (e.ColumnIndex == this.Column17.Index)  
    {  
        DataGridViewComboBoxCell dgcb = (DataGridViewComboBoxCell)dataGridView7[e.ColumnIndex, e.RowIndex];  

        dgcb.DataSource = unfilteredSecondaryBS;  
        this.filteredSecondaryBS.RemoveFilter();
    }  
}  





任何帮助都是gr8,知道如果选择了streetName,文本框将被填充第二个cmbx



我尝试了什么:



两个组合框工作正常,但我在ISFsectionid的文本框中需要帮助



any help would be gr8,knowing that textboxes will be filled if streetName is selected in the 2nd cmbx

What I have tried:

the two comboboxes are working fine but i need help in the ISFsectionid's textboxes

推荐答案

你可以使用DataGridView.CellValueChanged event [ ^ ],检查列是否与街道名称列匹配并在文本框中填入所需的值。



这里我假设您有另一个数据表,您可以使用streetID作为键选择值。 />


这样的东西:

首先你需要一个DataTable或您可以在其中查找streetId的词典。

You could use the DataGridView.CellValueChanged event[^], check if the column matches the street name column and fill the text boxes with desired values.

Here I assume that you have another data table from where you can select a value using the streetID as the key.

Something like this:
First you need a DataTable or a Dictionary where you can look up streetId.
DataTable dtWhatEver = new DataTable("WhatEver");
DataColumn dcStreetId = dtWhatEver.Columns.Add("streetId", typeof(int));
dtWhatEver.PrimaryKey = new DataColumn { dcStreetId };
dtWhatEver.Columns.Add("isfsectionId1", typeof(int));
dtWhatEver.Columns.Add("isfsectionId2", typeof(int));



在此表中填入数据库中的数据或其他来源。



[UPDATE]行和列索引的切换位置

然后实现类似于此的内容


Fill this table with data from your database or other source.

[UPDATE] Switched places of row and column index
Then implement something similar to this

private void dataGridView_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
    if ((e.RowIndex > -1) && (e.ColumnIndex == 1)) // Assuming this is the streetId
    {
        int streetId = dataGridView[e.ColumnIndex, e.RowIndex].Value;
        DataRow drFound = dtWhatEver.Rows.Find(streetId);
        if (drFound == null)
        {
            // Do some error handling
        }
        else
        {
            dataGridView[3, e.RowIndex].Value = drFound["isfsectionId1"];
            dataGridView[4, e.RowIndex].Value = drFound["isfsectionId2"];
        }
    }
}





免责声明

我在这里直接编写了这段代码,因此可能会有一些语法错误或缺少类型转换。

你应该明白这个想法。



Disclaimer
I wrote this code directly here, so there might be a few syntax errors or missing type conversions.
You should get the idea, though.


我发现了这个问题,它是

dataGridView7 [2,e.rowindex] .Value = drFound [fasileOne];

not

dataGridView7 [e.RowIndex,2] .Value = drFound [fasileOne];

列然后行
i found the issue,it was
dataGridView7[2 , e.rowindex].Value = drFound["fasileOne"];
not
dataGridView7[e.RowIndex, 2].Value = drFound["fasileOne"];
columns then rows


这篇关于如何根据组合框列选择在datagridview中填充同一行中的两个文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆