0

大家好,我使用 VC#2008 编写了一个代码,允许用户根据需要使用是/否对话框DataRows 结果使用索引按顺序输入许多。dataTable

首先,用户使用文本框和组合框使用函数输入他的数据,然后 dilodresult 弹出“你想输入更多记录吗?” 如果是,请清除文本框并调用函数并加上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;
            }
        }
4

1 回答 1

0

我不明白问题到底是什么,但似乎您正在尝试向 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();
}
于 2014-09-28T10:40:26.157 回答