Click here to Skip to main content
15,899,679 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
here is y sceenario.

first i have a checkedlistbox and datagridview.
second my requirement is to load the languages into checkedlistbox with languages.
then depending on the checked language, language details such as text and id are displayed in the datagridview.

i have written the code as follows,
C#
form_load();
sqlcommand cmd=new sqlcommand("select id,originaltext from texts",con);
sqldataadapter adp=new sqldataadapter(cmd);
datatable dt =new datatable();
adp.fill(dt);
if(dt.rows.count>0)
{
    datagraidview1.datasource=dt;
}

checkeditemlist_selectindexchanged()
{
    sqlcommand cmd=new sqlcommand("select id,originaltext from texts where language='japan'",con)
    sqldataadapter adp=new sqldataadapter(cmd);
    datatable dt=new datatable();
    if(dt.rows.count>0)
    {
        datagridview1.datasource=dt;
    }
}

problem:
i want to display the selected language beside the default language displayed.
can anyone help me.
Posted
Updated 21-Nov-15 4:31am
v2
Comments
OriginalGriff 21-Nov-15 8:15am    
Please do not post a new question to add information - use the "Improve Question" widget instead.
I'll delete the old one in this case.
Member 11807562 21-Nov-15 8:20am    
its ok, i understand but i need the logic to solve the problem.its urgent.
OriginalGriff 21-Nov-15 9:57am    
It's also a weekend, so you may have to have patience...
George Jonsson 21-Nov-15 21:07pm    
Be advised. This question is urgent only to you.
The rest of us have a lazy Sunday.

1 solution

You can merge two data tables if you want.
A new data column will be added to the original data table, provided the primary keys are the same.

Declare a DataTable as a member variable in your class
C#
private DataTable dt1;


In this case I didn't have a database so I initialized the data table myself.

C#
public Form1()
{
    InitializeComponent();

    dt1 = new DataTable();

    DataColumn dcPrimary = dt1.Columns.Add("ID", typeof(int));
    dt1.Columns.Add("OriginalText", typeof(string));
    dt1.PrimaryKey = new DataColumn[] { dcPrimary };

    dt1.Rows.Add(1, "Hello world.");
    dt1.Rows.Add(2, "Goodbye cruel world.");

    dataGridView1.DataSource = dt1.DefaultView;
}


Then in an update method you do like this
C#
private void AddLanguage(object sender, EventArgs e)
{
    DataTable dt2 = new DataTable();
    DataColumn dcPrimary = dt2.Columns.Add("ID", typeof(int));
    dt2.Columns.Add("AlternativeText", typeof(string));
    dt2.PrimaryKey = new DataColumn[] { dcPrimary };

    dt2.Rows.Add(1, "Hejsan världen.");
    dt2.Rows.Add(2, "Hejdå grymma värld.");

    dt1.Merge(dt2);
}

(I don't speak Japanese, so I used Swedish instead)

Of course this solution doesn't match your question completely, but I think you can modify the code and make it work.
 
Share this answer
 
Comments
Member 11807562 21-Nov-15 22:56pm    
hey thank you very much, i tried you code but im getting a new column but im unable to fecth the records into rows from database. can you help me with this?
George Jonsson 21-Nov-15 22:58pm    
Help you with the SQL?
I thought you had that covered.
I don't have time right now, so see this site http://www.w3schools.com/sql/default.asp[^]
Member 11807562 21-Nov-15 23:08pm    
thnaks for the link,it helps

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