0

所以我通过一个存储过程访问一个数据库,并且能够将我想要的数据拉到一个弹出框中。现在的问题是,无论我点击哪一行,相同的数据都会不断填充我的字段。我怎么能选择一个特定的行,并从数据库中提取该特定行的数据?谢谢你的帮助

表格1

public partial class DSC_Mon : Form
{
    DSCU_SvcConsumer.MTCaller Caller;
    DataSet dsZA;
    DataSet dsOut;

    public DSC_Mon()
    {
        InitializeComponent();
        Caller = new DSCU_SvcConsumer.MTCaller();
        dsZA = new DataSet();
        dsOut = new DataSet();
    }

    private void DSC_Mon_Load(object sender, EventArgs e)
    {
        timer1.Enabled = true;
    }

    private void LoadData()
    {
        if (!dsZA.Tables.Contains("Query"))
        {
            DataTable dtZA = dsZA.Tables.Add("Query");
            dtZA.Columns.Add("str");
            dtZA.Rows.Add(string.Format(@"exec MON_GetStatus"));
        }
        //dtZA.Rows.Add(string.Format(@"select * from am_company"));

        dsOut.Clear();
        dsOut.Merge(Caller.CallRequest("ZuluAction", dsZA));
        if (gridEXMon.DataSource == null)
        {
            gridEXMon.DataSource = dsOut;
            gridEXMon.DataMember = "Table";
        }
        //gridEXMon.RetrieveStructure();
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        timer1.Enabled = false;
        LoadData();
        timer1.Enabled = true;
    }

    private void gridEXMon_DoubleClick(object sender, EventArgs e)
    {
        ReportInfo report = new ReportInfo();
        report.ShowDialog();
    }
}

我将私有 void gridEXMon_DoubleClick 替换为:

private void gridEXMon_RowDoubleClick(object sender, Janus.Windows.GridEX.RowActionEventArgs e)
    {
        if (e.Row.RowIndex < 0)
            return;

        int rowIndex = Convert.ToInt32(e.Row.RowIndex);

        ReportInfo report = new ReportInfo(rowIndex);
        report.ShowDialog();
    }

弹出对话框

public partial class ReportInfo : Form
{
    public ReportInfo()
    {
        InitializeComponent();

        DSCU_SvcConsumer.MTCaller caller = new DSCU_SvcConsumer.MTCaller();

        DataSet dsZA = new DataSet();
        DataSet dsOut = new DataSet();
        if (!dsZA.Tables.Contains("Query"))
        {
            DataTable dtZA = dsZA.Tables.Add("Query");
            dtZA.Columns.Add("str");
            dtZA.Rows.Add(string.Format(@"MON_ReportInfo"));
        }

        dsOut.Clear();
        dsOut.Merge(caller.CallRequest("ZuluAction", dsZA));

        DataTable dt = dsOut.Tables["Table"];
        DataRow dr = dt.Rows[0];
        if (dt != null)
        {
            systemNameTextBox.Text = dr["System"].ToString();
            contactName1TextBox.Text = dr["Manager"].ToString();
            functionNameTextBox.Text = dr["Function"].ToString();
            durationMSTextBox.Text = dr["Speed"].ToString();
        }
    }

然后我将 rowIndex 发送到弹出窗口:

        DataTable dt = dsOut.Tables["Table"];
        DataRow dr = dt.Rows[rowIndex];
        systemNameTextBox.Text = dr["System"].ToString();
        contactName1TextBox.Text = dr["Manager"].ToString();
        functionNameTextBox.Text = dr["Function"].ToString();
        durationMSTextBox.Text = dr["Speed"].ToString();
    }
4

2 回答 2

1

最好从网格中获取数据,以便在 ReportInfo 类中保存对数据库的额外调用。

这是代码:

private void gridEXMon_DoubleClick(object sender, EventArgs e)
    {
        if (e.Row.RowIndex < 0)
            return;

        ReportInfo report = new ReportInfo();

        String System = Convert.ToInt32(e.Row.Cells["System"].Value);
        String Manager = Convert.ToString(e.Row.Cells["Manager"].Value);
        String Function = Convert.ToDecimal(e.Row.Cells["Function"].Value);
        String Speed = Convert.ToInt32(e.Row.Cells["Speed"].Value);

        report.ShowDialog(System, Manager, Function, Speed);
    }

那么你的 ReportInfo 类 ctor 应该是:

public partial class ReportInfo : Form
{
    public ReportInfo(String System, String Manager, String Function, String Speed)
    {
        InitializeComponent();

        systemNameTextBox.Text = System;
        contactName1TextBox.Text = Manager;
        functionNameTextBox.Text = Function;
        durationMSTextBox.Text = Speed;
    }
}
于 2014-11-12T02:40:27.107 回答
0

您是否尝试过:

gridEXMon.DataSource = dsOut.Tables[0];
// next line is not required if you've already defined proper grid structure 
gridEXMon.RetrieveStructure(); 

Janus 在这里也有他自己的论坛。你可以在那里找到很多有用的信息。对于浏览,我将使用 IE。Janus 论坛仅适用于 IE...

编辑:

班级如何ReportInfo知道您选择了哪些记录?特别是你有闲置的代码:

DataTable dt = dsOut.Tables["Table"];
DataRow dr = dt.Rows[0]; // always first record is taken!

可能您应该使用行索引定义 ctor:

public ReportInfo(int rowIndex)
{
    ...
    DataTable dt = dsOut.Tables["Table"];
    DataRow dr = dt.Rows[rowIndex];
    ....
}
于 2014-11-11T19:21:05.247 回答