Click here to Skip to main content
15,880,469 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C#
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();
    }  
}  


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
Posted
Updated 7-Jun-16 13:46pm
v2
Comments
George Jonsson 6-Jun-16 19:00pm    
What do you want to fill the text box with?
ramy nemer 7-Jun-16 3:29am    
with isfsectionId1 and isfsectionId2 (foreign keys for isfsectionId in isfsections)
i have the table street :
streetId streetName areaId isfsectionId1 isfsectionId2
1 street1 3 8 5
2 street2 5 4 null

i need when i click streetname in comboboxcolumn index 1 the two textboxes is filled automatically with isfSectionName

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.
C#
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));

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
C#
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.
 
Share this answer
 
v3
Comments
ramy nemer 7-Jun-16 6:21am    
sorry can you give any suggestion or a simple code of how do it that please
George Jonsson 7-Jun-16 6:51am    
See my updated answer. That is all I have time for right now.
ramy nemer 7-Jun-16 9:26am    
thanks george , but i have the tblsecondary having all columns,i marked the primary key in it, when running the program its giving me that this table doesnt have a primary key why ?
ramy nemer 7-Jun-16 9:42am    
private void dataGridView7_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
if ((e.RowIndex > -1) && (e.ColumnIndex == 1)) // Assuming this is the streetId
{
int streetId = Convert.ToInt16(dataGridView7.Rows[e.RowIndex].Cells[e.ColumnIndex].Value);
DataRow drFound = tblSecondary.Rows.Find(streetId);
if (drFound == null)
{
MessageBox.Show("ddhhdhd");
}
else
{
dataGridView7[e.RowIndex, 2].Value = drFound["fasileOne"];
dataGridView7[e.RowIndex, 3].Value = drFound["fasileTwo"];
}
}

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);
tblSecondary.PrimaryKey = new DataColumn[] { tblSecondary.Columns["streetId"] };

i changed the position of the primary key,its working but an exception occurs :
Index was out of range. Must be non-negative and less than the size of the collection.
George Jonsson 7-Jun-16 9:48am    
Maybe indicate where the error occurs.
i found the issue,it was
dataGridView7[2 , e.rowindex].Value = drFound["fasileOne"];
not
dataGridView7[e.RowIndex, 2].Value = drFound["fasileOne"];
columns then rows
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900