大家好,我使用VC#2008编写了一个代码,允许用户根据需要使用yes/no dialogesult通过对dataTable
的索引连续输入许多DataRows
。
首先,用户使用文本框和组合框输入数据,然后弹出"你想输入更多记录吗?"如果是,清除文本框并调用函数,加上index
,并在datagridview、but it doesnot show any data at the datagridviewand the dialogResult countinue showing without performing anything
中显示数据
这是功能代码:
public DataTable showout2(int index, bool bl)
{
DataTable dtab = new DataTable();
DataRow row = dtab.NewRow();
string[] newRow = new string[14];
try
{
DataColumn dc1 = new DataColumn("رقم المتسلسل");
DataColumn dc2 = new DataColumn("رقم الحساب");
DataColumn dc3 = new DataColumn("أسم الحساب");
DataColumn dc4 = new DataColumn("المالك");
DataColumn dc5 = new DataColumn("تاريخ");
DataColumn dc6 = new DataColumn("قيمة");
DataColumn dc7 = new DataColumn("نوع العملة");
DataColumn dc8 = new DataColumn("البيان");
DataColumn dc9 = new DataColumn("الدائن");
DataColumn dc10 = new DataColumn("المدين");
DataColumn dc11 = new DataColumn(" اجمالي الدائن");
DataColumn dc12 = new DataColumn("اجمالي المدين");
DataColumn dc13 = new DataColumn("تفاصيل");
DataColumn dc14 = new DataColumn("التأكيد");
dtab.Columns.Add(dc1);
dtab.Columns.Add(dc2);
dtab.Columns.Add(dc3);
dtab.Columns.Add(dc4);
dtab.Columns.Add(dc5);
dtab.Columns.Add(dc6);
dtab.Columns.Add(dc7);
dtab.Columns.Add(dc8);
dtab.Columns.Add(dc9);
dtab.Columns.Add(dc10);
dtab.Columns.Add(dc11);
dtab.Columns.Add(dc12);
dtab.Columns.Add(dc13);
dtab.Columns.Add(dc14);
DateTime date = new DateTime();
date = DateTime.Today;
row[0] = numb.Text.ToString();
row[1] = Account_numb.Text.ToString();
row[2] = account_nam.Text.ToString();
row[3] = owner.Text;
row[4] = curency.Text.ToString();
row[5] = date.ToString();
row[6] = curncyval.Text;
row[7] = note.Text;
row[8] = Depet.Text;
row[9] = criedt.Text;
row[10] = Total_depet.Text;
row[11] = Total_credit.Text;
row[12] = Details.Text;
row[13] = "false";
dtab.Rows.InsertAt(row, index);
dataGridView1.DataSource = dtab;
dataGridView1.DataSource = dtab;
DialogResult dialogResult = MessageBox.Show("تريد أدخال سجل أخر؟", "Some Title", MessageBoxButtons.YesNo);
if (dialogResult == DialogResult.Yes)
{
row[0] = numb.Text.ToString();
row[1] = Account_numb.Text.ToString();
row[2] = account_nam.Text.ToString();
row[3] = owner.Text;
row[4] = curency.Text.ToString();
row[5] = date.ToString();
row[6] = curncyval.Text;
row[7] = note.Text;
row[8] = Depet.Text;
row[9] = criedt.Text;
row[10] = Total_depet.Text;
row[11] = Total_credit.Text;
row[12] = Details.Text;
row[13] = "false";
numb.Clear();
owner.Clear();
Account_numb.Text = " ";
account_nam.Text = " ";
curency.Text = " ";
curncyval.Clear();
Total_credit.Clear();
Total_depet.Clear();
note.Clear();
Details.Clear();
criedt.Clear();
Depet.Clear();
return showout2(++index, true);
}
else if (dialogResult == DialogResult.No)
{
return dtab;
}
}
我不明白到底是什么问题,但似乎你正在尝试向datagridview添加行,添加新行后,你会问用户"他是否不想添加更多?"
首先需要在应用程序加载时或在构造函数中初始化datagridview一次。
public void initDatagrid()
{
dataGridView1.Columns.Add(new DataGridViewTextBoxColumn { HeaderText = "رقم المتسلسل" });
dataGridView1.Columns.Add(new DataGridViewTextBoxColumn { HeaderText = "رقم الحساب" });
dataGridView1.Columns.Add(new DataGridViewTextBoxColumn { HeaderText = "أسم الحساب" });
dataGridView1.Columns.Add(new DataGridViewTextBoxColumn { HeaderText = "المالك" });
dataGridView1.Columns.Add(new DataGridViewTextBoxColumn { HeaderText = "تاريخ" });
dataGridView1.Columns.Add(new DataGridViewTextBoxColumn { HeaderText = "قيمة" });
dataGridView1.Columns.Add(new DataGridViewTextBoxColumn { HeaderText = "نوع العملة" });
//...
}
然后调用此函数添加行
public void showout2()
{
object[] newRow = new object[14];
try
{
newRow[0] = "test";
newRow[1] = "test";
newRow[2] = "test";
newRow[3] = "test";
newRow[4] = "test";
newRow[5] = "test";
newRow[6] = "test";
//...
dataGridView1.Rows.Add(newRow);
if (MessageBox.Show("تريد أدخال سجل أخر؟", "Some Title", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
{
//clear data here ..
}
}
catch (Exception ex)
{
}
}
例如:
public yourClass()
{
InitializeComponent();
initDatagrid();
}
private void AddButton_Click(object sender, EventArgs e)
{
showout2();
}