从数据网格视图中选择一部分数据c#

时间:2015-05-01 15:08:24

标签: c# datagridview datasource

我正在尝试从数据网格视图中选择一部分数据,鼠标拖动从文本文件中获取信息,然后使用此选定数据更新数据网格视图。

更新:

无法清除网格视图的初始错误已解决,现在当所选数据尝试重新输入网格时,此行文本将出现在数据网格中,其中数据应为: “DataGridViewRow {Index = -1}”

foreach (string row in rows)
            {
                //Adds time to the array
                var items = new List<string> { timeS };
                items.AddRange(row.Split(delimeter.ToArray()));

                speed = Convert.ToDouble(items[2]);
                //converts 'speed' to a double and divides by 10 to display the correct value
                speed = speed / 10;
                items[2] = speed.ToString();
                dataset.Tables[tableName].Rows.Add(items.ToArray());
                //Adds the interval time to Dtime for each row
                Dtime = Dtime.AddSeconds(inter);

            }
            //selects where to display the data
            this.dataGridView1.DataSource = dataset.Tables[0].DefaultView;
        }

以下是在datagridview中显示信息的初始代码:

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.EditText;


public class startwith2player extends ActionBarActivity {
private boolean playerone = true , playertwo = false ;
private Integer score1 = 0 , score2 = 0;
private Integer flag4 = 0 , flag5 = 0 , flag6 = 0 , flag7 = 0, flag8 = 0, flag9 = 0 , flag10 = 0, flag11 = 0, flag12 = 0;
private Integer counter1 = 0 , counter2 = 0;
TextView tex1 ;
TextView tex2 ;
String firstplayer = "";
String secondplayer = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_startwith2player);
     tex1 = (TextView) findViewById(R.id.textView3);
     tex2 = (TextView) findViewById(R.id.textView4);
    EditText player1 = (EditText)findViewById(R.id.editText);
    EditText player2 = (EditText)findViewById(R.id.editText2);
    firstplayer = player1.getText().toString();
    secondplayer = player2.getText().toString();
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_startwith2player, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
public void refresh(){
    BlueAllBoxes();
    if(playerone){
        playerone = false;
        playertwo = true;
    }
    else{
    playerone = true;
    playertwo = false;
    }
    flag4 = 0 ; flag5 = 0 ; flag6 = 0 ; flag7 = 0; flag8 = 0; flag9 = 0 ; flag10 = 0; flag11 = 0; flag12 = 0;
    counter1 = 0 ; counter2 = 0;
}

public void BlueAllBoxes(){
    Button button4 = (Button) findViewById(R.id.button4);
    button4.setBackgroundColor(0xFF4B4BFF);
    Button button5 = (Button) findViewById(R.id.button5);
    button5.setBackgroundColor(0xFF4B4BFF);
    Button button6 = (Button) findViewById(R.id.button6);
    button6.setBackgroundColor(0xFF4B4BFF);
    Button button7 = (Button) findViewById(R.id.button7);
    button7.setBackgroundColor(0xFF4B4BFF);
    Button button8 = (Button) findViewById(R.id.button8);
    button8.setBackgroundColor(0xFF4B4BFF);
    Button button9 = (Button) findViewById(R.id.button9);
    button9.setBackgroundColor(0xFF4B4BFF);
    Button button10 = (Button) findViewById(R.id.button10);
    button10.setBackgroundColor(0xFF4B4BFF);
    Button button11 = (Button) findViewById(R.id.button11);
    button11.setBackgroundColor(0xFF4B4BFF);
    Button button12 = (Button) findViewById(R.id.button12);
    button12.setBackgroundColor(0xFF4B4BFF);
}
public void button4(View view) {
    if(counter1 == 3){
        if(flag4 == 1) {
            view.setBackgroundColor(0xFF67FF5A);
            counter2++;

            if(playerone) {
                score1 = score1 + 30;
               // tex1.setText(String.valueOf(score1));
                tex1.setText(firstplayer + " : " + score1 );
            }
            else {
                score2 = score2 + 30;
                tex2.setText(secondplayer + " : " + score2 );
            }
            if(counter2 == 3)
            refresh();
        }
        else{
            view.setBackgroundColor(0xFFFF231E);
            if(playerone) {
                score1 = score1 - 10;
                tex1.setText(firstplayer + " : " + score1 );
            }
            else {
                score2 = score2 - 10;
                tex2.setText(secondplayer + " : " + score2 );
            }
        }
    }
 else {
        if (flag4 == 0) {
            flag4 = 1;
            view.setBackgroundColor(0xFF67FF5A);
            counter1++;
            if(counter1 == 3)
              BlueAllBoxes();
        }
    }
}
public void button5(View view) {
    if(counter1 == 3){
        if(flag5 == 1) {
            view.setBackgroundColor(0xFF67FF5A);
            counter2++;

               if(playerone) {
                    score1 = score1 + 30;
                   tex1.setText(firstplayer + " : " + score1 );
                }
               else {
                   score2 = score2 + 30;
                   tex2.setText(secondplayer + " : " + score2 );
                }
                if(counter2 == 3)
                refresh();
        }
        else{
            view.setBackgroundColor(0xFFFF231E);
            if(playerone) {
                score1 = score1 - 10;
                tex1.setText(firstplayer + " : " + score1 );
            }
            else {
                score2 = score2 - 10;
                tex2.setText(secondplayer + " : " + score2 );
            }
        }
    }
    else {
        if (flag5 == 0) {
            flag5 = 1;
            view.setBackgroundColor(0xFF67FF5A);
            counter1++;
            if(counter1 == 3)
                BlueAllBoxes();
        }
    }
}
public void button6(View view) {
    if(counter1 == 3){
        if(flag6 == 1) {
            view.setBackgroundColor(0xFF67FF5A);
            counter2++;

            if(playerone) {
                score1 = score1 + 30;
                tex1.setText(firstplayer + " : " + score1 );
            }
            else {
                score2 = score2 + 30;
                tex2.setText(secondplayer + " : " + score2 );
            }
                if(counter2 == 3)
                refresh();
        }
        else{
            view.setBackgroundColor(0xFFFF231E);
            if(playerone) {
                score1 = score1 - 10;
                tex1.setText(firstplayer + " : " + score1 );
            }
            else {
                score2 = score2 - 10;
                tex2.setText(secondplayer + " : " + score2 );
            }
        }
    }
    else {
        if (flag6 == 0) {
            flag6 = 1;
            view.setBackgroundColor(0xFF67FF5A);
            counter1++;
            if(counter1 == 3)
                BlueAllBoxes();
        }
    }
}
public void button7(View view) {
    if(counter1 == 3){
        if(flag7 == 1) {
            view.setBackgroundColor(0xFF67FF5A);
            counter2++;

            if(playerone) {
                score1 = score1 + 30;
                tex1.setText(firstplayer + " : " + score1 );
            }
            else {
                score2 = score2 + 30;
                tex2.setText(secondplayer + " : " + score2 );
            }
            if(counter2 == 3)
                refresh();
        }
        else{
            view.setBackgroundColor(0xFFFF231E);
            if(playerone) {
                score1 = score1 - 10;
                tex1.setText(firstplayer + " : " + score1);
            }
            else {
                score2 = score2 - 10;
                tex2.setText(secondplayer + " : " + score2 );
            }
        }
    }
    else {
        flag7 = 1;
        view.setBackgroundColor(0xFF67FF5A);
        counter1++;
        if(counter1 == 3)
            BlueAllBoxes();
    }
}
public void button8(View view) {
    if(counter1 == 3){
        if(flag8 == 1) {
            view.setBackgroundColor(0xFF67FF5A);
            counter2++;

            if(playerone) {
                score1 = score1 + 30;
                tex1.setText(firstplayer + " : " + score1 );
            }
            else {
                score2 = score2 + 30;
                tex2.setText(secondplayer + " : " + score2 );
            }
            if(counter2 == 3)
                refresh();
        }
        else{
            view.setBackgroundColor(0xFFFF231E);
            if(playerone) {
                score1 = score1 - 10;
                tex1.setText(firstplayer + " : " + score1 );
            }
            else {
                score2 = score2 - 10;
                tex2.setText(secondplayer + " : " + score2 );
            }
        }
    }
    else {
        flag8 = 1;
        view.setBackgroundColor(0xFF67FF5A);
        counter1++;
        if(counter1 == 3)
            BlueAllBoxes();
    }
}
public void button9(View view) {
    if(counter1 == 3){
        if(flag9 == 1) {
            view.setBackgroundColor(0xFF67FF5A);
            counter2++;

            if(playerone) {
                score1 = score1 + 30;
                tex1.setText(firstplayer + " : " + score1 );
            }
            else {
                score2 = score2 + 30;
                tex2.setText(secondplayer + " : " + score2 );
            }
            if(counter2 == 3)
                refresh();
        }
        else{
            view.setBackgroundColor(0xFFFF231E);
            if(playerone) {
                score1 = score1 - 10;
                tex1.setText(firstplayer + " : " + score1 );
            }
            else {
                score2 = score2 - 10;
                tex2.setText(secondplayer + " : " + score2 );
            }
        }
    }
    else {
        flag9 = 1;
        view.setBackgroundColor(0xFF67FF5A);
        counter1++;
        if(counter1 == 3)
            BlueAllBoxes();
    }
}
public void button10(View view) {
    if(counter1 == 3){
        if(flag10 == 1) {
            view.setBackgroundColor(0xFF67FF5A);
            counter2++;

            if(playerone) {
                score1 = score1 + 30;
                tex1.setText(firstplayer + " : " + score1 );
            }
            else {
                score2 = score2 + 30;
                tex2.setText(secondplayer + " : " + score2 );
            }
            if(counter2 == 3)
                refresh();
        }
        else{
            view.setBackgroundColor(0xFFFF231E);
            if(playerone) {
                score1 = score1 - 10;
                tex1.setText(firstplayer + " : " + score1 );
            }
            else {
                score2 = score2 - 10;
                tex2.setText(secondplayer + " : " + score2 );
            }
        }
    }
    else {
        flag10 = 1;
        view.setBackgroundColor(0xFF67FF5A);
        counter1++;
        if(counter1 == 3)
            BlueAllBoxes();
    }
}
public void button11(View view) {
    if(counter1 == 3){
        if(flag11 == 1) {
            view.setBackgroundColor(0xFF67FF5A);
            counter2++;

            if(playerone) {
                score1 = score1 + 30;
                tex1.setText(firstplayer + " : " + score1 );
            }
            else {
                score2 = score2 + 30;
                tex2.setText(secondplayer + " : " + score2 );
            }
            if(counter2 == 3)
                refresh();
        }
        else{
            view.setBackgroundColor(0xFFFF231E);
            if(playerone) {
                score1 = score1 - 10;
                tex1.setText(firstplayer + " : " + score1 );
            }
            else {
                score2 = score2 - 10;
                tex2.setText(secondplayer + " : " + score2);
            }
        }
    }
    else {
        flag11 = 1;
        view.setBackgroundColor(0xFF67FF5A);
        counter1++;
        if(counter1 == 3)
            BlueAllBoxes();
    }
}
public void button12(View view) {
    if(counter1 == 3){
        if(flag12 == 1) {
            view.setBackgroundColor(0xFF67FF5A);
            counter2++;

            if(playerone) {
                score1 = score1 + 30;
                tex1.setText(firstplayer + " : " + score1 );
            }
            else {
                score2 = score2 + 30;
                tex2.setText(secondplayer + " : " + score2);
            }
            if(counter2 == 3)
                refresh();
        }
        else{
            view.setBackgroundColor(0xFFFF231E);
            if(playerone) {
                score1 = score1 - 10;
                tex1.setText(firstplayer + " : " + score1 );
            }
            else {
                score2 = score2 - 10;
                tex2.setText(secondplayer + " : " + score2);
            }
        }
    }
    else {
        flag12 = 1;
        view.setBackgroundColor(0xFF67FF5A);
        counter1++;
        if(counter1 == 3)
            BlueAllBoxes();
    }
}
 }

任何帮助将不胜感激!

干杯。

1 个答案:

答案 0 :(得分:1)

您无法清除DataGridView.Rows,因为您已指定了数据源。 您必须清除DataGridView的DataSource。

而不是

dataGridView1.Rows.Clear();

写道:

dataset.Tables[0].Clear();

修改 您的代码中有错误...您无法在表中添加DataGridViewRow 此代码可以解决您的问题:

    foreach (DataGridViewRow row in rowCollection)
    {
        DataRow r =  dataset.Tables[tableName].NewRow();
        //
        //write the data in the DataRow and then add the datarow in your           datatable
        dataset.Tables[tableName].Rows.Add(r);

    }