views:

311

answers:

2

Hi, i'm a long-time newbie to c#, and this question may be too obvious, so please forgive if i'm "doing it wrong."

Using Visual Studio 2005 Pro, C#, SQL Server 2000 EE SP3.

I have a stored proc that takes one input param and returns several output params pertaining to that input. I am also calling it successfully, and String.Format-ing the outputs into a RichTextBox. I'd like to call this proc periodically and each time stick the output params into a DataGridView row, so the results accumulate over time.

  • What's the simplest/most elegant way to do get those parameters into the DataGridView? I'd prefer code instead of some GUI builder solution.
  • Am i overlooking some better approach, such as a more appropriate control?

Thanks!

+1  A: 
//set up the datatable
DataTable dt = new DataTable("parms");
dt.Columns.Add("ParmName",typeof(string));
dt.Columns.Add("ParmValue",typeof(string));
//bind to a gui object
myDataGridView.DataSource = dt;

//do this after each sproc call
foreach (SqlParameter parm in cmd.Parameters)
{
    if (parm.Direction == ParameterDirection.Output)
    {
        //do something with parm.Value
        dt.Rows.Add(new object [] { parm.ParameterName, 
            Convert.ToString(parm.Value) } );
    }
}
Steven A. Lowe
Thanks Steven. However, i was actually more interested in the the GUI side of things. I.e. how hook up the proc output to the datagridview, or some better control.
bill weaver
@[bill weaver]: see edits
Steven A. Lowe
With a couple modifications (since i know the specific columns i want) it works great. Adding my code to another answer for completeness. Thanks again!
bill weaver
A: 

Steve's answer got me close. I'm not sure a DataGridView is the best solution, but it seems to be working.

In Form(), after InitializeComponent(), i put the column setup code (since i want to keep adding rows periodically):

dgvOutput.Columns.Add("@col1", "Val 1");
dgvOutput.Columns.Add("@col2", "Val 2");
dgvOutput.Columns.Add("@col3", "Val 3");

And in the button handler, after calling the stored proc

dgvOutput.Rows.Add(new object[]{
    Convert.ToString(cmd.Parameters["@col1"].Value),
    Convert.ToString(cmd.Parameters["@col2"].Value),
    Convert.ToString(cmd.Parameters["@col3"].Value)
});
dgvOutput.AutoResizeColumns();
bill weaver