如何在C#中清除旧值时显示不同的数据 [英] How Do I Display Different Data With Out Clearing Old Values In C#

查看:90
本文介绍了如何在C#中清除旧值时显示不同的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是y sceenario。



首先我有一个checkedlistbox和datagridview。

秒我的要求是将语言加载到checkedlistbox使用语言。

然后根据检查的语言,在datagridview中显示语言详细信息,如text和id。



i已写入代码如下,

 form_load(); 
sqlcommand cmd = new sqlcommand( select id,来自文本的原始文本,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( 选择id,来自文本的原始文本,其中language ='japan',con)
sqldataadapter adp = new sqldataadapter(cmd);
datatable dt = new datatable();
if (dt.rows.count> 0)
{
datagridview1.datasource = dt;
}
}



问题:

我希望在显示的默认语言旁边显示所选语言。

任何人都可以帮助我。

解决方案

如果需要,你可以合并两个数据表。

新的数据列将被添加到原始数据表中,前提是主键是相同的。



声明 DataTable 作为班级中的成员变量

  private  DataTable dt1; 





在这种情况下我没有数据库所以我自己初始化了数据表。



< pre lang =C#> public Form1()
{
InitializeComponent();

dt1 = new DataTable();

DataColumn dcPrimary = dt1.Columns.Add( ID,< span class =code-keyword> typeof ( int ));
dt1.Columns.Add( OriginalText typeof string ));
dt1.PrimaryKey = new DataColumn [] {dcPrimary};

dt1.Rows.Add( 1 Hello world。);
dt1.Rows.Add( 2 再见残酷的世界。);

dataGridView1.DataSource = dt1.DefaultView;
}





然后在更新方法中你喜欢这个

 私有  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 Hejsanvärlden。);
dt2.Rows.Add( 2 Hejdågrymmavärld。);

dt1.Merge(dt2);
}



(我不会说日语,所以我用瑞典语代替)



当然这个解决方案完全不符合您的问题,但我认为您可以修改代码并使其正常工作。


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,

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.

解决方案

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

private DataTable dt1;



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

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

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.


这篇关于如何在C#中清除旧值时显示不同的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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