问题描述
1)具有照片,播放器名称,年龄,棒球风格,保龄球风格,国家/地区等字段.
2)我需要将数据更新到datagriedview表中.
3)玩家名用于控制文本框和组合框.
4)在datagridview中,我添加一列复选框.
5)如何删除复选框中选中的内容?
6)如何在组合框播放器名称中选择剩余名称,详细信息应在相应的文本框内查看
1)having fields like photo,playername,age,battingstyle,bowlingstyle,country.
2)i need to update the data into datagriedview table.
3)playername used controls textbox and combobox.
4)In datagridview i add one column checkbox.
5)how to delete selected in checkbox?
6)how can i selected in combobox playername remaing details should view in there respective textbox''s
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form3 : Form
{
DataTable table= new DataTable();
DataTable dt1;
DataRow dr;
public Form3()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
openFileDialog1.ShowDialog();
button1.Text = openFileDialog1.FileName;
pictureBox1.ImageLocation = button1.Text;
button1.Text = "Browse";
}
private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
{
}
private void button4_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Really Quit?", "Exit", MessageBoxButtons.OKCancel) == DialogResult.OK)
{
Application.Exit();
}
}
private void pictureBox1_Click(object sender, EventArgs e)//picturebox
{
}
private void button2_Click(object sender, EventArgs e)//add button
{
//DataTable dt = new DataTable("Cricket Players");
//DataTable dt = new DataTable();
//DataRow dr;
//dt.Columns.Add("Photos", typeof(string));
//dt.Columns.Add("PlayerName", typeof(string));
//dt.Columns.Add("Age", typeof(Int32));
//dt.Columns.Add("BattingStyle", typeof(string));
//dt.Columns.Add("BowlingStyle", typeof(string));
//dt.Columns.Add("Country", typeof(string));
//dt.Columns.Add("SelectionBox", typeof(bool));
bool a = true;
if (pictureBox1.Image == null)
{
errorProvider6.SetError(pictureBox1, "please attacth image");
a = false;
}
else
{
errorProvider6.Clear();
}
if (textBox1.Text == "")
{
errorProvider1.SetError(textBox1, "please enter name");
a = false;
}
else
{
errorProvider1.Clear();
}
if (textBox2.Text ==string.Empty)
{
errorProvider2.SetError(textBox2, "please enter Age");
a = false;
}
else
{
errorProvider2.Clear();
}
if (textBox3.Text == "")
{
errorProvider3.SetError(textBox3, "please enter BattingStyle");
a = false;
}
else
{
errorProvider3.Clear();
}
if (textBox4.Text == "")
{
errorProvider4.SetError(textBox4, "please enter BowlingStyle");
a = false;
}
else
{
errorProvider4.Clear();
}
if (textBox5.Text == "")
{
errorProvider5.SetError(textBox5,"please enter country");
a = false;
}
else
{
errorProvider5.Clear();
}
if (a == true)
{
dr = dt1.NewRow();
dr["Photos"] = Image.FromFile(pictureBox1.ImageLocation);
dr["PlayerName"] = textBox1.Text;
dr["Age"] = Convert.ToInt32(textBox2.Text);
dr["BattingStyle"] = textBox3.Text;
dr["BowlingStyle"] = textBox4.Text;
dr["Country"] = textBox5.Text;
dt1.Rows.Add(dr);
this.dataGridView1.Visible = true;
dataGridView1.DataSource = dt1;
}
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)//datagridview
{
}
private void Form3_Load(object sender, EventArgs e)
{
comboBox1.Visible = false;
dt1 = new DataTable();
dataGridView1.DataSource = dt1;
dt1.Columns.Add("Photos", typeof(Image));
dt1.Columns.Add("PlayerName", typeof(string));
dt1.Columns.Add("Age", typeof(Int32));
dt1.Columns.Add("BattingStyle", typeof(string));
dt1.Columns.Add("BowlingStyle", typeof(string));
dt1.Columns.Add("Country", typeof(string));
dt1.Columns.Add("SelectionBox", typeof(bool));
//for (int i = 0; i < dataGridView1.Rows.Count; i++)
//{ // dataGridView1.Rows.RemoveAt(i);
//}
}
private void button3_Click(object sender, EventArgs e)//delete button
{
for (int i = 0; i <= dataGridView1.RowCount - 1; i++)
{
if (dataGridView1.Rows[i].Cells["SelectionBox"].Selected == true)
//if (dataGridView1.Rows[i].Cells[6].Value =="1")//sıfırın
{
dataGridView1.Rows.RemoveAt(i);
}
}
}
private void button5_Click(object sender, EventArgs e)//new button
{
foreach (Control X in this.Controls)
{
if (X is TextBox)
{
(X as TextBox).Text = "";
}
else if (X is PictureBox)
{
(X as PictureBox).Image = null;
}
}
}
private void button7_Click(object sender, EventArgs e)//update button
{
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)//combobox
{
}
private void button6_Click(object sender, EventArgs e) //update button
{
textBox1.Visible = false;
comboBox1.Visible = true;
}
}
}
推荐答案
for (int i = dataGridView1.RowCount -1; i <= 0; i--)
{
if (dataGridView1.Rows[i].Cells["SelectionBox"].Selected == true)
{
dataGridView1.Rows.RemoveAt(i);
}
}
您粘贴了大量代码,但没有给我们任何反馈,因此,如果这不能解决您的问题,请使用改进"问题,并向我们提供其他信息. :)
You pasted a large set of code, and you have not given us any feedback, so if this does not solve your problem then use the Improve question and give us additional information. :)
这篇关于使用C#将datagridview与数据表绑定.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!