如何在"添加"时添加行到WPF dataGrid按钮点击?

时间:2015-05-07 04:45:56

标签: c# wpf wpfdatagrid datarow

这里使用下面的代码向wpf datagrid添加行,但如果再次点击添加意味着无法添加行而前一行被新添加的详细信息替换....那么如何才能添加新的更多基于添加按钮的行在外部单击。 这里的代码是一个使用,它只在datagrid中添加了一行,所以如何通过外部按钮点击更多新的添加行。

    private void AddButton_Click(object sender, RoutedEventArgs e)
    {

        DataTable dt = new DataTable();
        DataRow dr = dt.NewRow();
        if (!dt.Columns.Contains("Department"))
        {
            dt.Columns.Add("Department");
        }
        if (!dt.Columns.Contains("ScanTest"))
        {
            dt.Columns.Add("ScanTest");
        }
        if (!dt.Columns.Contains("Doctor"))
        {
            dt.Columns.Add("Doctor");
        }
        if (!dt.Columns.Contains("Date"))
        {
            dt.Columns.Add("Date");
        }
        if (!dt.Columns.Contains("Rate"))
        {
            dt.Columns.Add("Rate");
        }
        dr["Department"] = comboBox4.Text.ToString();
        dr["ScanTest"] = comboBox5.Text.ToString();
        dr["Doctor"] = comboBox6.Text.ToString();
        dr["Date"] = datePicker1.SelectedDate.Value;
        dr.ItemArray[0] = comboBox4.Text.ToString();
        dr.ItemArray[1] = comboBox5.Text.ToString();
        dr.ItemArray[2] = comboBox6.Text.ToString();

        dataGrid1.ItemsSource = dt.DefaultView;
        dt.Rows.Add(dr);
}

1 个答案:

答案 0 :(得分:2)

每次单击该按钮时,都会创建一个新的private SoundPool mSoundPool; private int mSoundID; private boolean isLoaded = false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.d(TAG, "Start Greeting View Activity..."); setContentView(R.layout.greeting_view_activity); //mGiGiView = (GreetingWidget) findViewById(R.id.gigi_greet); //mGiGiView.setOnTouchListener(this); //Set default animation sound path. String soundAnimUrl = "/gigi/anim/evening.ogg"; // get the Bundle out of the Intent. Bundle extras = getIntent().getExtras(); if (extras != null) { // check to see if "soundAnimUrl" is in the bundle, if so then // assign it's value to animUrl if not, assign null to soundAnimUrl. soundAnimUrl = extras.containsKey("soundAnimUrl") ? extras .getString("soundAnimUrl") : null; } // Set the hardware buttons to control the music. this.setVolumeControlStream(AudioManager.STREAM_MUSIC); // Load the sound. mSoundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0); mSoundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() { @Override public void onLoadComplete(SoundPool soundPool, int sampleId, int status) { isLoaded = true; // Play the sound when loaded play(); } }); mSoundID = mSoundPool .load(getFile(Environment.DIRECTORY_MUSIC, soundAnimUrl) .getPath(), 1); //Play sound from raw directory // soundID = soundPool.load(this, R.raw.greeting1, 1); } private void play() { // Getting the user sound settings AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE); float actualVolume = (float) audioManager .getStreamVolume(AudioManager.STREAM_MUSIC); float maxVolume = (float) audioManager .getStreamMaxVolume(AudioManager.STREAM_MUSIC); float volume = actualVolume / maxVolume; // Is the sound loaded already? if (isLoaded) { mSoundPool.play(mSoundID, volume, volume, 1, 0, 1f); Log.d(TAG, "Played sound"); } } @Override public boolean onTouch(View v, MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN) { switch (v.getId()) { case R.id.gigi_greet: play(); break; default: break; } } return false; } /** * Get File instance from sd card path. * * @param deviceFolderPath * - Pictures, Music, etc * @param dbFilePath * - path stored in db (/gigi/anim/morning.ogg) * @return */ public File getFile(final String deviceFolderPath, final String dbFilePath) { // Create full path String picturePath = deviceFolderPath.concat(File.separator).concat( dbFilePath); // Create file File mFile = getExternalFilesDir(picturePath); return mFile; } ,并且每次向其中添加新的单行。初始化此函数外的Data Table

Data Table