Dump stores proc output parameters into DataGridView row
I've been new to C # for a long time and this question may be too obvious, so please forgive me 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 parameter and returns multiple output parameters relative to that input. I also call this successfully, and String.Format is outputting the results to the RichTextBox. I would call this process periodically and insert the output parameters into the DataGridView row each time, so the results accumulate over time.
- What's the easiest / most elegant way to get these parameters in a DataGridView? I would prefer code instead of some GUI solution.
- Am I missing a better approach, like a more appropriate control?
Thank!
0
source to share
2 answers
//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) } );
}
}
+1
source to share
Steve answered me closely. I'm not sure if the DataGridView is the best solution, but it seems to work.
In Form (), after InitializeComponent (), I will put the column setting code (since I want to add 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 saved 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();
0
source to share