Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Dynamic Bound Column

using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class StockVendor : System.Web.UI.Page
{
int count1;

protected void Page_Load(object sender, EventArgs e)
{
SqlConnection Conn;
Conn = new SqlConnection("Data Source=AB2\\SQLEXPRESS;Initial Catalog=Tiger;Integrated Security=True");

Conn.Open();

// SqlCommand salCmd = new SqlCommand("select count(b.product_name),a.ven_name,b.ven_code from vendor_details a INNER join orders b on a.ven_code = b.ven_code and b.status = 'in process'group by b.ven_code,ven_name", Conn);
SqlCommand salCmd = new SqlCommand("select count(V.Vendor_name),V.Vendor_code,V.vendor_name,V.contact_name1,V.contact_mobile1 from vendor V INNER join stock S on S.vendor_code = V.Vendor_code group by V.vendor_name,V.Vendor_code,V.contact_name1,V.contact_mobile1", Conn);
SqlDataReader dr = salCmd.ExecuteReader();

while (dr.Read())
{
count1 = (int)dr[0];

string code = dr[1].ToString();
string name = dr[2].ToString();
string name1 = dr[3].ToString();
string mobile1 = dr[4].ToString();


SqlConnection conDataGrid = new SqlConnection("Data Source=AB2\\SQLEXPRESS;Initial Catalog=Tiger;Integrated Security=True");
SqlDataAdapter adapterDataGrid = new SqlDataAdapter("select prod_id,prod_name,quantity,inprice from stock where vendor_code ='" + code + "'", conDataGrid);
DataSet ds = new DataSet();
adapterDataGrid.Fill(ds);
//conDataGrid.Open();
// SqlCommand comStock = new SqlCommand("select prod_name,quantity,inprice from stock where vendor_code ='" + code + "'", conDataGrid);
//SqlDataReader drStock = comStock.ExecuteReader();
//while (drStock.Read())
//{
//string product = drStock[0];
//int qty = drStock[1];
//int price = drStock[2];

DataGrid dg = new DataGrid();
dg.Width = 600;
dg.GridLines = GridLines.Both;
dg.CellPadding = 1;
dg.ForeColor = System.Drawing.Color.Black;
dg.BackColor = System.Drawing.Color.Beige;
dg.AlternatingItemStyle.BackColor = System.Drawing.Color.Gainsboro;
dg.HeaderStyle.BackColor = System.Drawing.Color.Brown;
dg.DataSource = ds;
dg.AutoGenerateColumns = true;

BoundColumn datagridcol = new BoundColumn();
datagridcol.HeaderText = "ID";
datagridcol.Visible = true;
datagridcol.DataField = "prod_id";
dg.Columns.Add(datagridcol);

ButtonColumn selectcol = new ButtonColumn();
selectcol.ButtonType = ButtonColumnType.LinkButton;
selectcol.Text = "ChangeVendor";
selectcol.CommandName = "Select";
selectcol.

dg.Columns.Add(selectcol);

dg.DataBind();



PlaceHolder1.Controls.Add(dg);





}
Conn.Close();

}

public void dg_SelectedIndexChanged(object sender, DataGridCommandEventArgs e)
{

}


}

I have used this code Dynamic Datagrid and Dynamic bound columns .
Now I have to get the value of the prod_id column when the llinkbutton is
clicked.
I came to know using selectedIndexChanged for geting that value.
But i am not able to implement it please guide me for this
[4350 byte] By [Rathi] at [2007-11-11 8:45:55]